4.9 Declaring Tags

The following syntax declares the identifier tag as a structure, union, or enumeration tag. If this tag declaration is visible, a subsequent reference to the tag substitutes for the declared structure, union, or enumerated type. Subsequent references of the tag in the same scope (visible declarations) must omit the bracketed list. The syntax of a tag is:

struct tag { declarator-list }
union tag { declarator-list }
enum tag { enumerator-list }

If the tag is declared without the complete structure or union declaration, it refers to an incomplete type. Incomplete enumerated types are illegal. An incomplete type is valid only to specify an object where the type is not required; for example, during type definitions and pointer declarations. To complete the type, another declaration of the tag in the same scope (but not within an enclosed block), defines the content.

The following construction uses the tag test to define a self-referencing structure.

struct test {
 float height;
 struct test *x, *y, *z;
};

Once this declaration is given, the following declaration declares s to be an object of type struct test and sp to be a pointer to an object of type struct test :

struct test s, *sp;

Note
The keyword typedef can also be used in an alternative construction to do the same thing:
typedef struct test tnode;
struct test {
     float height;
     tnode *x, *y, *z;
};
tnode s, *sp;


Previous Page | Next Page | Table of Contents | Index