Unions are a concept borrowed from structures and therefore follow the same syntax as structures.However,there is major distinction between them of storage.In structures,each member has its own storage location,whereas all the members of a union use the same location.This implies that,although a union may contain many members of different type,it can handle only one member at a time.
a union can be declared using the keyword union as follows:
union item
{
int m;
float x;
char c;
}
code;
The declares a variable code of 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 due to the fact that only one location is allocated for a union variable,irrespective of its size.
The compiler allocated a piece of storage i.e large enough to hold the largest variable type in the union.
No comments:
Post a Comment