Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <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 type. It allows multiple members to share the
    same storage space. A union type is optionally named using a union
    tag. Each may have any data type, including other unions and
    structures, or be a function. Unlike classes, members in unions share
    the same memory space, and are public by default. 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
                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 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. This
                also applies to unions without tags.

                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));



See Also: class Types struct enum

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