COHERENT manpages
This page displays the COHERENT manpage for union [Multiply declare a variable].
List of available manpages
Index
union -- C Keyword
Multiply declare a variable
A union defines an area of storage that can accept any one of several types
of data. In effect, it is a multiple declaration of a variable. For
example, a union may be declared to consist of an int, a double, and a char
*. Any one of these three elements can be held by the union at a time, and
will be handled appropriately by it. For example, the declaration
union {
int number;
double bignumber;
char *stringptr;
} example;
allows example to hold either an int, a double, or a pointer to a char,
whichever is needed at the time. All of these have the same address. The
elements of a union are accessed like those of a struct: for example, to
access number from the above example, type example.number.
unions are helpful in dealing with heterogeneous data, especially within
structures; however, you are responsible for keeping track of what data
type the union is holding at any given time. Passing a double to a union
and then reading the union as though it held an int will yield results that
are unpredictable, and probably unwelcome.
Example
For an example of how to use a union in a program, see the entry for byte
ordering.
See Also
C keywords,
initialization,
struct,
structure
ANSI Standard, §3.1.2.5, §3.5.2.1

















