Device Monitoring Studio - Monitor, log and analyze data coming through PC ports and connections
Download Device Monitoring Studio Hide this button

Unions

A union is a combination of data fields. In contrast to a structure, all union data fields are located at the same address and, therefore, share the same storage space. The size of the union equals to the size of the largest field, and alignment of the union equals the largest field's alignment.

A union definition consists of zero or more of data fields:

type var-decl [, var-decl…];

where var-decl is:

(id | id[array-size-expression] | id:bit-field-size-expression | id as type-id *)

A union may contain different fields with a same name. If such a field is referenced in expression, an [] operator may be used to address individual fields. If this operator is not used, the first field is referenced.

Typedefs, constants, enumerations, native functions and nested user-defined types are also allowed within a union definition.

Plain field, array field, bit field and pointer field are described in more detail in their corresponding sections.

Example:

union A
{
    int intVal;
    short shortVal;
    double dblVal;
    char charArray[4];
    struct
    {
        int a;
        __int64 b;
    } nestedStruct;
};