UNION:-
- Unions like structures, contain members whose individual data types may different from another.
- However the members within a union all share the same storage area within the computer’s memory, whereas each member within a structure is assigned its own unique storage area.
- Thus, unions are used to save memory.
- They are useful for applications involving multiple members, where values need not be assigned to all of the members at any one time.
Syntax of declaration Union:
union<union_name>
{
<variable_list>;
};
Object of Union:
union<union_name><object_name>;
e.g.
union Demo
{
int a;
char ch[30];
float b;
Difference between union and structure:
1) Structure:-
—We can access all the members of structure at anytime.
—Memory is allocated for all variables.
—All members of structure can be initialized.
2) UNION:-
—Only one member of union can be accessed at anytime.
—Allocates memory for variable which variable require more memory.
—Initialized Only the first member of a union can be initialized.
0 comments:
Post a Comment