Unions like structure contain members whose individual data types may differ from one another. However the members that compose a union all share the same storage area within the computers memory where as each member within a structure is assigned its own unique storage area. Thus unions are used to observe memory. They are useful for application involving multiple members. Where values need not be assigned to all the members at any one time. Like structure union can be declared using the keyword union as follows :
union item
{
int m;
float p;
char c;
}
code;
This declares a variable code to type union item. The union contains three members each with a different data type. However we can use only one of them at a time. This is because if only one location is allocated for union variable irrespective of time. The compiler allocates a piece of storage that is large enough to access a union member we can use the same syntax that we use to access structure members. That is-
code.m
code.p
code.c
are all valid member variables.
union item
{
int m;
float p;
char c;
}
code;
This declares a variable code to type union item. The union contains three members each with a different data type. However we can use only one of them at a time. This is because if only one location is allocated for union variable irrespective of time. The compiler allocates a piece of storage that is large enough to access a union member we can use the same syntax that we use to access structure members. That is-
code.m
code.p
code.c
are all valid member variables.
0 Comments:
Post a Comment