Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ 3.0r4 - <b>union union data type</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
union                    Union Data Type

    A union is an aggregate data type. It allows multiple data objects to
    share the same storage space. A union type is optionally named using
    a union tag. Each object in a union is called a member; a member may
    have any data type, including other unions and structures. A union
    may contain any one of its member objects at a time. To define the
    layout of a union, optionally give that layout a tag name, and define
    variables of that type, use the following format:

           union [tag] {
                member-1 declaration;
                ...
                member-n declaration;
           } [declarator [, declarator ... ]] ;

    Once a union layout has been defined, it may be referenced in
    subsequent declarations using the format:

           union tag declarator [, declarator ...] ;

    In either case,the declarator may contain an initializer list which
    is interpreted according to the type of the first member specified.

    When members are referenced, they must be qualified by their parent
    union name, as in:

           union_name1.member_name1 = value1;
           union_name2.member_name2 = value2;

    and since unions may be nested, the general naming format becomes:

     level-1.level-2. ... .member

      Notes:    If a union layout has no tag it cannot be referenced in
                subsequent (and separate) declarations, casts or function
                definitions.

                Unions may be nested and may contain members of any data
                type. A union may not contain an instance of itself;  it
                may, however, contain a pointer to a union of the same
                type, something commonly done in defining linked-list
                nodes.

                Unions with different tag names define different data
                types even if their layouts are identical. That is, such
                unions are not assignment-compatible and should not be
                copied to or compared with each other. If you really mean
                two union types to be the same, make them the same. Tis
                also applies to unions without tags.

                Since unions may contain structures, unions may have
                "holes" due to alignment requirements of the host
                architecture or efficiency considerations. Therefore,
                whenever you need to deal with the length of a union, use
                sizeof(union) rather than a hard-coded size. MS-C aligns
                all objects on word boundaries, except chars, which are
                byte-aligned. The size of a union is the size needed to
                store its largest member.

                Each member starts at the same address, which is equal to
                the address of the union.

                Each union has its own identifier namespace; a union tag
                must be distinct from other enum, struct and union tags.

                A union may be assigned into using the assignment
                operator =.  A union may be passed by value to functions,
                and returned by value from functions.

                A union of class auto may not have an initializer list.

  -------------------------------- Example ---------------------------------

           union symbol {
                int int_constant;
                double fp_constant;
                char *identifier_name;
           } symbol_table[1000];.
           union symbol table_entry, *table_ptr, entry[100];

           table_ptr = (union symbol) malloc(10 * sizeof(union symbol));


  -------------------------------- Example ---------------------------------

    /* These register structures and unions can be found in <dos.h>. */

    /* Word registers */

           struct WORDREGS
           {
                unsigned int ax;
                unsigned int bx;
                unsigned int cx;
                unsigned int dx;
                unsigned int si;
                unsigned int di;
                unsigned int cflag;
           };


            /* Byte registers */

           struct BYTEREGS
           {
                unsigned char al, ah;
                unsigned char bl, bh;
                unsigned char cl, ch;
                unsigned char dl, dh;
           };


            /* Union REGS overlays WORDREGS and BYTEREGS. */

           union  REGS
           {
                struct WORDREGS x;
                struct BYTEREGS h;
           };

See Also: Types struct enum

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson