Loading, please wait...

Unions

A union is a derived data type like a structure with members that share the same storage space. For different situations in a program, some variables may not be relevant, but other variables are so a union shares the space instead of wasting storage on variables that are not being used. The members of a union can be of any data type.

 

The number of bytes used to store a union must be at least enough to hold the largest member. In most cases, unions contain two or more data types. Only one member, and thus one data type, can be referenced at a time. It is your responsibility to ensure that the data in a union is referenced with the proper data type.

 

 Referencing data in a union with a variable of the wrong type is a logic error. If data is stored in a union as one type and referenced as another type, the results are implementation dependent.