C/C++ union, struct vs union
An union is same as structures in which all members are stored at the same memory address. That means, all members cannot be there at the same time. At a time, only one member can be stored in such an object.
The main purpose of unions is to prevent memory fragmentation by arranging a standard size for data in the memory. Hence, it provides a way to manipulate different kinds of data in a single storage area.
Defining a union in C/C++ programming
The union keyword is used to define union. The syntax is given below
union Union_name { data-typename variable1; data-typename variable2; . . . . data-typename variableN; };
for example:
union Student{ int id; char name[20]; };
Declaring union variable
Following is the syntax to declare union variable.
union Union_name union_variable;
This is written inside the main() function.
For example
union student S1,S2;
At different time the program is to treat S1 and S2 as being either integer or character types.
Accessing members of union
To access members of union, we use dot operator like structure. The syntax is given below.
union_variable . member_name;
For example consider the above student union in which assign id value for S1 object.
S1.id =100;
The program sees S1 as being an integer type. Suppose when s1 refers to name as
S1.name = "krivalar";
Now program treats S1 as being array of character
Using Union in C/C++ Programming - Example Program
Following is a simple example program using union.
#include<stdio.h> /*define Student union */ union Student { int id; char name[20]; char branch[10]; float mark; }; void main() { union Student S1; //declare S1 variable for Student union clrscr(); //initialize member of the Student union S1.id = 1; strcpy(S1.name, "krishna"); strcpy(S1.branch, "CSE"); S1.mark = 70; //print the student details printf("\n The student id is: %d ",S1.id); printf("\n The name is: %s ",S1.name); printf("\n The branch is: %s ",S1.branch); printf("\n The mark is: %f ",S1.mark); }
Outpuut
The student id is: The name is: The branch is: The mark is:70.000000
As you can see that the value of id, name and branch are not printed but only the final mark value is printed because at a time only one member can occupies the memory location.
Following is a simple example program using union.
#include<stdio.h> /*define Student union */ union Student { int id; char name[20]; char branch[10]; float mark; }; void main() { union Student S1; //declare S1 variable for Student union clrscr(); //initialize member of the Student union //print the student details S1.id = 1; printf("\n The student id is: %d ",S1.id); strcpy(S1.name, "krishna"); printf("\n The name is: %s ",S1.name); strcpy(S1.branch, "CSE"); printf("\n The branch is: %s ",S1.branch); S1.mark = 70; printf("\n The mark is: %f ",S1.mark); }
Difference between structure and union
Structure | Union |
---|---|
Collection of one or more variables of different data types that are grouped together under single name. | Contains number of variable with different data types like structure but it holds only one object at a time |
Each member occupies its own memory location | All members use the same memory location. |
The size of structure variable is equal to sum of all members size. | The size of union variable is equal to size of largest member. |
For example: struct Student{ int i; char name[10]; }s1; size of s1 =1+10=12 bytes | For example: union Student{ int i; char name[10]; }s1; size of s1=10 bytes (largest member size) |
strcat() - String Concatenation strcmp() - String Compare
strcpy() - String Copy strlen() - String Length