Unions: A Quick Look
A union is a type that enables you to store different data types in the same memory space (but not simultaneously). A typical use is a table designed to hold a mixture of types in some order that is neither regular nor known in advance. With a union, you can create an array of equal-sized units, each of which can hold a variety of data types.
Unions are set up in much the same way as structures. There is a union template and a union variable. They can be defined in one step or, by using a union tag, in two. Here is an example of a union template with a tag:
union hold { int digit; double bigfl; char letter; };
Here is an example of defining three union variables of the hold type:
union hold fit; /* union variable of hold ...
Get C Primer Plus®, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.