.TH union "" "" "C Keyword"
.PC "Multiply declare a variable"
.PP
A
.B 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
.B union
may be declared to consist of an
.BR int ,
a
.BR double ,
and a
.BR "char *" .
Any one of these three elements can be held by the
.B union
at a time, and will be handled appropriately by it.
For example, the declaration
.DM
union {
	int number;
	double bignumber;
	char *stringptr;
} example;
.DE
.PP
allows
.B example
to hold either an \fBint\fR, a \fBdouble\fR, or a pointer to a \fBchar\fR,
whichever is needed at the time.
All of these have the same address.
The elements of a \fBunion\fR are accessed like those of a \fBstruct\fR:
for example, to access \fBnumber\fR from the above example, type
\fBexample.number\fR.
.PP
.BR union s
are helpful in dealing with heterogeneous data, especially
within structures; however, you are responsible
for keeping track of what data type the
.B union
is holding at any given time.
Passing a
.B double
to a
.B union
and then reading the
.B union
as though it held an
.B int
will yield results that are unpredictable, and probably unwelcome.
.SH "Example"
For an example of how to use a
.B union
in a program, see the entry for
.BR "byte ordering" .
.SH "See Also"
.Xr "C keywords," c_keyword
.Xr "initialization," initializ
.Xr "struct," struct
.Xr "structure" structur
.br
\*(AS, \(sc3.1.2.5, \(sc3.5.2.1
