C language union
In the
C programming language, a
union is a datatype. The union may consist of different fields, each with different types. The space taken up by the union is the same as the largest of the individual types (plus padding) - and all the fields within the union have the same address as the union itself. For example
- union {
- int a;
- double b;
- } c;
Will make a union called 'c', which has an integer component and a double component. Either of these can be used, but if 'a' is updated, 'b', will be clobbered, and vice versa. According to the C standard, it is invalid to read 'b' if 'a' was last written to, although on many implementations this gives useful behaviour.
See also :
Mathematical union - Set theoretic union.