联合声明Union Declarations

“联合声明”指定一组变量值和(可选)一个命名联合的标记。A "union declaration" specifies a set of variable values and, optionally, a tag naming the union. 变量值称为联合的“成员”,并且可以具有不同的类型。The variable values are called "members" of the union and can have different types. 联合类似于其他语言中的“变体记录”。Unions are similar to "variant records" in other languages.

语法Syntax

struct-or-union-specifier:struct-or-union-specifier:
struct-or-union identifier opt{ struct-declaration-list }struct-or-union identifier opt{ struct-declaration-list }

struct-or-union identifierstruct-or-union identifier

struct-or-union:struct-or-union:
structstruct

unionunion

struct-declaration-list:struct-declaration-list:
struct-declarationstruct-declaration

struct-declaration-list struct-declarationstruct-declaration-list struct-declaration

联合内容定义为The union content is defined to be

struct-declaration:struct-declaration:
specifier-qualifier-list struct-declarator-list ;specifier-qualifier-list struct-declarator-list ;

specifier-qualifier-list:specifier-qualifier-list:
type-specifier specifier-qualifier-list opttype-specifier specifier-qualifier-list opt

type-qualifier specifier-qualifier-list opttype-qualifier specifier-qualifier-list opt

struct-declarator-list:struct-declarator-list:
struct-declaratorstruct-declarator

struct-declarator-list , struct-declaratorstruct-declarator-list , struct-declarator

带 union 类型的变量存储该类型所定义的值之一。A variable with union type stores one of the values defined by that type. 相同的规则可控制结构和联合声明。The same rules govern structure and union declarations. 联合还可以具有位域。Unions can also have bit fields.

联合的成员不能包含不完整类型、类型 void 或函数类型。Members of unions cannot have an incomplete type, type void, or function type. 因此,成员不能是联合的实例,但可以是指向将声明的联合类型的指针。Therefore members cannot be an instance of the union but can be pointers to the union type being declared.

联合类型声明只是一个模板。A union type declaration is a template only. 不保留内存,直到声明变量。Memory is not reserved until the variable is declared.

备注

如果声明两个类型的联合并存储一个值,但使用其它类型访问该联合,则结果是不可靠的。If a union of two types is declared and one value is stored, but the union is accessed with the other type, the results are unreliable. 例如,声明 float 和 int 的联合。For example, a union of float and int is declared. 存储一个 float 值,但程序稍后会将该值作为 int 进行访问。A float value is stored, but the program later accesses the value as an int. 在这种情况下,值取决于 float 值的内部存储。In such a situation, the value would depend on the internal storage of float values. 整数值是不可靠的。The integer value would not be reliable.

示例Examples

下面是联合的示例:The following are examples of unions:

union sign   /* A definition and a declaration */  
{  
    int svar;  
    unsigned uvar;  
} number;  

此示例定义了带 sign 类型的联合变量,并声明了一个名为 number 的变量,该变量包含两个成员:svar(一个带符号整数)和 uvar(一个无符号整数)。This example defines a union variable with sign type and declares a variable named number that has two members: svar, a signed integer, and uvar, an unsigned integer. 此声明允许将 number 的当前值存储为带符号的值或无符号的值。This declaration allows the current value of number to be stored as either a signed or an unsigned value. 与此联合类型关联的标记为 signThe tag associated with this union type is sign.

union               /* Defines a two-dimensional */  
{                   /*  array named screen */  
    struct      
    {   
      unsigned int icon : 8;    
      unsigned color : 4;  
    } window1;  
    int screenval;  
} screen[25][80];  

screen 数组包含 2,000 个元素。The screen array contains 2,000 elements. 数组的每个元素均为一个包含以下两个成员的联合:window1screenvalEach element of the array is an individual union with two members: window1 and screenval. window1 成员是带两个位域成员(iconcolor)的结构。The window1 member is a structure with two bit-field members, icon and color. screenval 成员是一个 intThe screenval member is an int. 在任意给定时间,每个联合元素保留由 int 表示的 screenval 或由 window1 表示的结构。At any given time, each union element holds either the int represented by screenval or the structure represented by window1.

Microsoft 专用Microsoft Specific

如果嵌套的联合是另一个结构或联合的成员,则可以匿名声明嵌套的联合。Nested unions can be declared anonymously when they are members of another structure or union. 这是一个无名称联合的示例:This is an example of a nameless union:

struct str  
{  
    int a, b;  
    union            / * Unnamed union */  
    {  
      char c[4];  
      long l;  
      float f;  
   };  
   char c_array[10];  
} my_str;  
.  
.  
.  
my_str.l == 0L;  /* A reference to a field in the my_str union */  

联合通常嵌套在一个结构中,该结构包含一个在任意特定时间给定联合中包含的数据类型的字段。Unions are often nested within a structure that includes a field giving the type of data contained in the union at any particular time. 这是此类联合的声明的示例:This is an example of a declaration for such a union:

struct x  
{  
    int type_tag;  
    union  
    {  
      int x;  
      float y;  
    }  
}  

有关引用联合的信息,请参阅结构和联合成员See Structure and Union Members for information about referencing unions.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

声明符和变量声明Declarators and Variable Declarations