6.7.2.3 Tags

Previous Table of Contents "New C Standard" commentary

1454 A specific type shall have its content defined at most once.

1455 Where two declarations that use the same tag declare the same type, they shall both use the same choice of struct, union, or enum.

1456 A type specifier of the form

        enum identifier

without an enumerator list shall only appear after the type it specifies is complete.

1457 All declarations of structure, union, or enumerated types that have the same scope and use the same tag declare the same type.

1458 The type is incomplete109) until the closing brace of the list defining the content, and complete thereafter.

1459 Two declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types.

1460 Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type.

1461 A type specifier of the form

        struct-or-union identifieropt { struct-declaration-list }

or

        enum identifier { enumerator-list }

or

        enum identifier { enumerator-list , }

declares a structure, union, or enumerated type.

1462 The list defines the structure content, union content, or enumeration content.

1463 If an identifier is provided,110) the type specifier also declares the identifier to be the tag of that type.

1464 A declaration of the form

        struct-or-union identifier ;

specifies a structure or union type and declares the identifier as a tag of that type.111

1465 109) An incomplete type may only by used when the size of an object of that type is not needed.

1466 It is not needed, for example, when a typedef name is declared to be a specifier for a structure or union, or when a pointer to or a function returning a structure or union is being declared. (See incomplete types in 6.2.5.)

1467 The specification has to be complete before such a function is called or defined.

1468 110) If there is no identifier, the type can, within the translation unit, only be referred to by the declaration of which it is a part.

1469 Of course, when the declaration is of a typedef name, subsequent declarations can make use of that typedef name to declare objects having the specified structure, union, or enumerated type.

1470 111) A similar construction with enum does not exist.

1471 If a type specifier of the form

        struct-or-union identifier

occurs other than as part of one of the above forms, and no other declaration of the identifier as a tag is visible, then it declares an incomplete structure or union type, and declares the identifier as the tag of that type.111

1472 If a type specifier of the form

        struct-or-union identifier

or

        enum identifier

occurs other than as part of one of the above forms, and a declaration of the identifier as a tag is visible, then it specifies the same type as that other declaration, and does not redeclare the tag.

1473 EXAMPLE 1 This mechanism allows declaration of a self-referential structure.


        struct tnode {
                int count;
                struct tnode *left, *right;
        };

specifies a structure that contains an integer and two pointers to objects of the same type. Once this declaration has been given, the declaration


        struct tnode s, *sp;

declares s to be an object of the given type and sp to be a pointer to an object of the given type. With these declarations, the expression sp->left refers to the left struct tnode pointer of the object to which sp points; the expression s.right->count designates the count member of the right struct tnode pointed to from s.

The following alternative formulation uses the typedef mechanism:


        typedef struct tnode TNODE;
        struct tnode {
                int count;
                TNODE *left, *right;
        };
        TNODE s, *sp;

1474 EXAMPLE 2 To illustrate the use of prior declaration of a tag to specify a pair of mutually referential structures, the declarations


        struct s1 { struct s2 *s2p; /* ... */ }; // D1
        struct s2 { struct s1 *s1p; /* ... */ }; // D2

specify a pair of structures that contain pointers to each other. Note, however, that if s2 were already declared as a tag in an enclosing scope, the declaration D1 would refer to it, not to the tag s2 declared in D2. To eliminate this context sensitivity, the declaration


        struct s2;

may be inserted ahead of D1. This declares a new tag s2 in the inner scope; the declaration D2 then completes the specification of the new type.

1475 Forward references: declarators (6.7.5), array declarators (6.7.5.2), type definitions (6.7.7).

Next

Created at: 2008-01-30 02:39:43 The text from WG14/N1256 is copyright © ISO