Union
Union is also a data structure just like structure which can store multiple variables of different type. But only difference is that in case of union memory is allocated only for the largest data member and shared by other data member also. It means same memory is shared by all the variables of union.
—–>image
Declaration of union
union tag
{
members;
};
tag define the name of union and members are the elements of union tag.
union declaration example
union test
{
int a;
int b;
}
above declaration declare a union of name test. which has two member integer a and b.
to create a single variable of type union below is the syntax
union tag variable_name;
example:-
union test x;
access union members
Union member can be accessed by (.) operator between the name of union variable and the member name.
tag.member;
example
test.a=10;