Union and Structure in C

Written by

Pooja Rao

Union and Structure:

What are they?

Structures and unions are a user-defined data type, also called as derived data type in C. These user-defined types gives us the flexibility to store values belonging to different data types under a single entity. They serve as a collection of different data types which are members of a particular group.

Where are they used?

Suppose, you need to store data of a particular class in your school. Your object would be a student, who will have various parameters associated with him/her like Name, Roll. No, Standard, Div, Subjects enrolled for, Gender, Address etc.

If you think of storing these values using a basic data type like array; you would have to create different arrays for all the parameters as the parameters have different data types; e.g: Roll. No is Integer, Name is string etc. This would pertain to a single student; for multiple students imagine the complexity of code that will get generated!

So Enter , Structures and Unions ! In this case you would just have to define either a structure / union for your object which is Student. The parameters of your object , become the members of the structure / union.
Thus, for every new student you just have to create a new variable of type Student!

Isn’t it much easier now ?

Aren’t they same then ? (Considering Structures and Unions will be explained individually and separately in another module)

Well, they are similar in certain aspects; not same.
First of all, the purpose is same which is to enable grouping of different data types under a single entity.

Syntax is same with the difference of keyword struct and union respectively.

When we need to access the values of the members, we do so using member access operator / dot operator / period (.) or pointer operator ‘->’.
Operators supported by them are sizeof() and assignment ‘=’ .

If they are not same, then what are the ddifferences?

Parameter Structure Union
Space Ocuupied Structures will occupy more memory space which will be equal to the sum of the space occupied by its members.
Example:
struct Student
{
int rollno;
char name[10];
float percentage;
}
Here, in all 18 bytes will be occupied as 4 bytes each for integer and float data type and 10 bytes for the string.
Union will occupy memory space equivalent to the space required by its largest member.
Example:
union Student
{
int rollno;
char name[10];
float percentage;
}
The largest member of this union is name; which is going to occupy 10 bytes only; instead of 18 as in structure.
Keyword for declaration struct union
Memory reference Each member points/refers to a different memory sspace/addressspace. Each member points/refers to the same memory/address space.
Member Values Values for all the members can be stored at a point in time. At a particular point in time, union stores only a single value for all its members as address space is shared amongst members.
Member Access Any member can be accessed at any point of time. Only a single member can be accessed at a time.
Member Initialization All the members are initialized simultaneously. Only the first member in a union is initialized.
Value modification Modification of a member’s value does not affect the other members. Modification of a member’s value modifies value of the other members.
Union and Structure in C