联合的存储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;  

x 联合的成员(按照其声明的顺序)是指向 char 值、char 值和 float 值的数组的指针。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. 由于 x 是联合的最长成员,因此为 f 分配的存储是 20 个元素数组 f 所需的存储。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

联合声明Union Declarations