Overview of Structures and Unions in C

Structures and unions are two of many user defined data types in C. Both are similar to each other but there are quite some significant differences. In this article, we will discuss about both of these data types and differentiate both these data types based on the functionalities.

STRUCTURES:

A structure simply can be defined as a user-defined data type which groups logically related items under one single unit. We can use all the different data items by accessing that single unit.

All the data item are stored in contiguous memory locations. It is not only permitted to one single data type items. It can store items of different data items. It has to be defined before using (Just like we define a variable before using it in the program).

Structure definition syntax:

Example:

Suppose we need to store the details of an employee. It includes ID, name, age, salary, designation and several other factors which belong to different data types.

Memory space allocation:

Let's assume we stored id . It is stores somewhere say in address 8000, it needs 2 memory units as it belongs to int data type. Now, the next item, name is stored after the id at 8002. In the same way, all the items are stores contiguously.

8000 -> emp_id (2 bytes)

8002 -> name[20] (20*1 = 20 bytes)

8022 -> salary (4 bytes)

8026 -> designation[50] (50*1 = 50 bytes)

8076 ->dept_no (2 bytes)

8078 -> age (2 bytes)

Example program:

Output:

Size of the structure: 14

Point to understand:

Here,

Size of Structure 'S' = sizeof(roll) + sizeof(name) + sizeof(mark)

= 2 + 10 + 2 = 14

Accessing the members of a structure:

We cannot initialize the structure in the definition and we cannot just access the members by using the names. After defining the structure, we need to define a variable of that structure data type.

Now, we use this variable to access the items of the structure using the special operator (.). Here is an example;

e1 is the structure variable now.

To access the data items in the structure:

  • name
  • salary
  • emp_id
  • address
  • dept_no

Here is another example:

Another way:

Example programs:

Output:

Roll no : 110
Name : Sarika
Marks : 568

//2. Input from user:

Output:

Enter the name of the student: Sagari
Enter his/her roll number: 6
Enter his/her marks: 780
Displaying the information:
Name: Sagari
Roll number: 6
Marks: 780

Assigning one structure to another:

We can assign one whole structure to another. If we do this, we can access the members of one structure using the other structure's variable and vice versa.

Let us assume e1 and e2 are the variables of two structures Emp1 and Emp2 respectively. If we assign e2 to e1:

Now, the values of every member of the structure Emp2 is assigned to the corresponding members of Emp1.

Array of structures:

It is used to store large number of similar info altogether. Here, we give an array as a structure variable rather than a normal variable. Now, we can store more info.

Syntax:

Implementation:

Output:

Enter detailsof the Employee
        Enter Employee Id : 1
        Enter Employee Name : Ravi
        Enter Employee Age : 21
        Enter Employee Salary : 23000

Enter detailsof the Employee
        Enter Employee Id : 2
        Enter Employee Name : Rajesh
        Enter Employee Age : 32
        Enter Employee Salary : 67890

Enter detailsof the Employee
        Enter Employee Id : 3
        Enter Employee Name : Kiran
        Enter Employee Age : 34
        Enter Employee Salary : 23435
Details of Eloyees
1       Ravi    21      23000
2       Rajesh  32      67890
3       Kiran   34      23435

Structures within a structure:

We can define a structure as another structure's variable. By doing this, we can create a separate block for a crucial member of the structure for storing more accurate and clear info.

Syntax:

Here, we took a variable doj which is another structure and included it in the structure date. By this, we get to know the exact date day, month and year thus increasing the readability and info.

To access these members,

The syntax to access the structure within another structure is

Example:

In the above example:

e1.doj.day; -> for day in date

e1.doj.month; -> for month in date

e1.doj.year; -> for year in month

Full program example:

Output:

employee id : 101
employee date of joining (dd/mm/yyyy) : 10-11-2014

UNION:

Just like a structure, a union is also a user-defined data type that groups together logically related variables into a single unit.

Almost all the properties and syntaxes are same, except some of the factors.

Syntax:

Example:

Memory allocation in a union:

Overview of Structures and Unions in C

In a structure, the memory occupied by it is simply the sum of the memory occupied by all its members as they are organized contiguously and every member has its own memory.

But, in a union, data items are organized one on another. So, the total memory space required to store a union is the memory required to store the largest memory required element in it.

Another major attachment here is that in a structure, we can access any number of items anytime. But, in a union, only one item can be accessed at a time.

Here is a table to differentiate all the factors between a structure and a union:

StructuresUnions
The keyword "struct" is used to define a structure.The keyword "union" is used to define a structure.
A unique memory location is given to every member of a structureOne single memory location is shared by all its members
Modifying the value of one item won't affect the other items or the structure at all.Modifying one single data item affects other members of the union thus affecting the whole unit.
We initialize several members at once.We can initialize only the first member of union.
The total size of the structure is the sum of the size of every data memberThe total size of the union is the size of the largest data member
It is mainly used for storing various data typesIt is mainly used for storing one of the many data types that are available.
It occupies space for each and every member written in inner parametersIt occupies space for a member having the highest size written in inner parameters
We can retrieve any member at any timeWe can only access one member at a time in the union.





Latest Courses