Union
Prev Next
C Union is also like structure,
i.e. collection of different data types which are grouped together. Each
element in a union is called member.
- Union and structure in C are same in concepts, except allocating memory for their members.
- Structure allocates storage space for all its members separately.
- Whereas, Union allocates one common storage space for all its members
- We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Where as Structure allocates storage space for all its members separately.
- Many union variables can be created in a program and memory will be allocated for each union variable separately.
- Below table will help you how to form a C union, declare a union, initializing and accessing the members of the union.
Type | Using normal variable | Using pointer variable |
Syntax | union tag_name { data type var_name1; data type var_name2; data type var_name3; }; |
union tag_name { data type var_name1; data type var_name2; data type var_name3; }; |
Example | union student { int mark; char name[10]; float average; }; |
union student { int mark; char name[10]; float average; }; |
Declaring union variable | union student report; | union student *report, rep; |
Initializing union variable | union student report = {100, “Mani”, 99.5}; | union student rep = {100, “Mani”, 99.5};report = &rep; |
Accessing union members | report.mark report.name report.average |
report -> mark report -> name report -> average |
Example program for C union:
Output:
Union record1 values example
Name : Subject : Percentage : 86.500000 Union record2 values example Name : Mani Subject : Physics Percentage : 99.500000 |
Explanation for above C union program:
There are 2 union variables declared in this program to understand the difference in accessing values of union members.
Record1 union variable:- “Raju” is assigned to union member “record1.name” . The memory location name is “record1.name” and the value stored in this location is “Raju”.
- Then, “Maths” is assigned to union member “record1.subject”. Now, memory location name is changed to “record1.subject” with the value “Maths” (Union can hold only one member at a time).
- Then, “86.50” is assigned to union member “record1.percentage”. Now, memory location name is changed to “record1.percentage” with value “86.50”.
- Like this, name and value of union member is replaced every time on the common storage space.
- So, we can always access only one union member for which value is assigned at last. We can’t access other member values.
- So, only “record1.percentage” value is displayed in output. “record1.name” and “record1.percentage” are empty.
- If we want to access all member values using union, we have to access the member before assigning values to other members as shown in record2 union variable in this program.
- Each union members are accessed in record2 example immediately after assigning values to them.
- If we don’t access them before assigning values to other member, member name and value will be over written by other member as all members are using same memory.
- We can’t access all members in union at same time but structure can do that.
Example program – Another way of declaring C union:
In this program, union variable “record” is declared while declaring union itself as shown in the below program.
Output:
Name :
Subject : Percentage : 86.500000 |
Note:
We can access
only one member of union at a time. We can’t access all member values at
the same time in union. But, structure can access all member values at
the same time. This is because, Union allocates one common storage space
for all its members. Where as Structure allocates storage space for all its members separately.
Difference between structure and union in C:
S.no |
C Structure
|
C Union
|
1 |
Structure allocates storage space for all its members separately.
|
Union allocates one common storage space for all its members. Union finds that which of its member needs high storage space over other members and allocates that much space |
2 | Structure occupies higher memory space. | Union occupies lower memory space over structure. |
3 | We can access all members of structure at a time. | We can access only one member of union at a time. |
4 | Structure example: struct student { int mark; char name[6]; double average; }; |
Union example: union student { int mark; char name[6]; double average; }; |
5 | For above structure, memory allocation will be like below. int mark – 2B char name[6] – 6B double average – 8B Total memory allocation = 2+6+8 = 16 Bytes |
For
above union, only 8 bytes of memory will be allocated since double data
type will occupy maximum space of memory over other data types. Total memory allocation = 8 Bytes |
No comments:
Post a Comment