Delen via


Union Declarations

A union is a structure-like type that can make memory use more efficient. For more information, see Union (C+).

A union declaration specifies a set of variable values and, optionally, a tag that names the union. The variable values are known as members of the union and can have different types. Unions resemble variant records in other languages.

Syntax

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

    struct-or-union identifier

  • struct-or-union:
    struct

    union

  • struct-declaration-list:
    struct-declaration

    struct-declaration-list struct-declaration

The union content is defined to be as follows.

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

  • specifier-qualifier-list:
    type-specifier specifier-qualifier-list opt

    type-qualifier specifier-qualifier-list opt

  • struct-declarator-list:
    struct-declarator

    struct-declarator-list , struct-declarator

A variable that has the union type stores one of the values that is defined by that type. The same rules govern structure and union declarations. Unions can also have bit fields.

Members of unions cannot have an incomplete type, the type void, or a function type. Therefore, members cannot be an instance of the union but can be pointers to the union type that is being declared.

A union type declaration is a template only. Memory is not reserved until the variable is declared.

Note

If a union of two types is declared and one type value is stored, but the union is accessed by using the other type, the results are unreliable. For example, a union of float and int is declared. A float value is stored, but the program later accesses the value as an int. In this situation, the value would depend on the internal storage of float values. The integer value would not be reliable.

Examples

This section shows examples of unions.

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

This example defines a union variable of type sign and declares a variable named number that has two members: svar, a signed integer, and uvar, an unsigned integer. This declaration enables the current value of number to be stored as either a signed value or an unsigned value. The tag that is 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];

The screen array contains 2,000 elements. Each element of the array is an individual union with two members: window1 and screenval. The window1 member is a structure that has two bit-field members, icon and color. The screenval member is an int. At any given time, each union element holds either the int represented by screenval or the structure represented by window1.

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 in a structure that includes a field that gives the type of data that is 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;
    }
}

For information about how to reference unions, see Structure and Union Members.

END Microsoft Specific

See Also

Concepts

Declarators and Variable Declarations