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

    A structure is an aggregate data type. It allows multiple data
    objects to be grouped together and named using a structure tag. Each
    object in a structure is called a member and may have any data type,
    including other structures and unions.

    A special member is the bit field. A bit field is an object whose
    size is measured in bits rather than bytes or words. Bit fields are
    useful for mapping into hardware registers and memory locations, and
    for packing binary flags into bytes or words without using the bit-
    wise operators |, & and ^.

    To define the layout of a structure, optionally give that layout a
    tag name, and to define variables of that type, use the following
    format:

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

The format of a bit field declaration is:

            type [identifier] : field-width;

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

            struct tag declarator [, declarator ...] ;

    In either case, the declarator may contain an initializer list.

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

            struct_name1.member_name1 = value1;
            struct_name2.member_name2 = value2;

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

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

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

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

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

                Structures 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
                structure, use sizeof(structure) rather than a hard-coded
                size. MS-C aligns all objects on word boundaries, except
                for chars, which by default are byte-aligned. Use the
                compile-time switch /Zp to get byte-boundary packing of
                other objects.

                Multiple bit fields should be declared adjacent to each
                other so they may be packed as densely as possible. MS-C
                packs bit fields into words, and trailing unused bits are
                wasted. It also packs bit fields into a word in order of
                their appearance. The first member has the lowest
                address. Bit fields cannot span int boundaries. MS-C only
                supports unsigned type bit fields; since "plain" int
                types are signed by default, you must specify the keyword
                unsigned.

                The bit field width must reduce to an unsigned integer
                constant expression at compile-time.  (It may be a macro
                name.) An unnamed bit field of width zero causes the next
                member to be aligned on the next int boundary.

                Unnamed bit fields are place-holders and are not
                accessible by name.

                Bit fields may only be defined inside of a structure. You
                cannot have arrays of bit fields, take their address, or
                use them with sizeof.

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

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

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

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

           struct date {
               int year;
               int month;
               int day;
           } todays_date = {87, 5, 1};

           struct date birth_date, *adate, dates[100];
               date = (struct date) malloc(10 * sizeof(struct date));


           /* Asynch communications adapter line control register */

           struct lcreg {
               unsigned int word_length   : 2;
               unsigned int stop_bits     : 1;
               unsigned int parity_enable : 1;
               unsigned int even_parity   : 1;
               unsigned int stick_parity  : 1;
               unsigned int set_break     : 1;
               unsigned int divisor_latch : 1;
           };

See Also: Types union enum Classes

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