Storage of Unions
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Storage of Unions

The storage associated with a union variable is the storage required for the largest member of the union. When a smaller member is stored, the union variable can contain unused memory space. All members are stored in the same memory space and start at the same address. The stored value is overwritten each time a value is assigned to a different member. For example:

union         /* Defines a union named x */
    char *a, b;
    float f[20];
} x;

The members of the x union are, in order of their declaration, a pointer to a char value, a char value, and an array of float values. The storage allocated for x is the storage required for the 20-element array f, since f is the longest member of the union. Because no tag is associated with the union, its type is unnamed or "anonymous."

See Also

© 2016 Microsoft