Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
union
◄Up► ◄Contents► ◄Index► ◄Back►
────────────────────────────────────────────────────────────────────────────
Keyword: union
Syntax: union [tag] {member-declaration-list} [declarators];
union tag declarators;
Summary: Declares a union type and/or a union variable.
If braces are given, the union declares a union type consisting of
a sequence of variable values (known as members of the union) that
can have different types. A variable of a union type can hold one
member of any type defined by the union. The size of the union is
the size of the largest type in the union. Variables can be
defined by listing their names after the closing brace.
If braces are not given, the union keyword is used to define a
union variable. The tag identifies the type. For example,
union UNKNOWN // Declare UNKNOWN union type
{
char ch; // Members of different types
int i;
long l;
float f;
double d;
} var1; // Variable of type UNKNOWN
union UNKNOWN var2; // Variable of type UNKNOWN
var1.i = 6; // Use variable as integer
var2.d = 5.327; // Use variable as double
A non-ANSI extension allows unions to be defined without
declarators when they are members of another struct or a union.
This allows the members to be referenced as if they were declared
directly in the containing struct or union. For example,
struct example
{
int a,b;
union {
char str[4];
long l;
float f;
};
char name[10];
} Example;
Example.l = 305;
-♦-