HP C/iX Reference Manual (31506-90011)
Chapter 3 37
Data Types and Declarations
Structure and Union Tags
Structure and Union Tags
Structures and unions are declared with the struct or union keyword. You can follow the
keywords with a tag that names the structure or union type much the same as an enum tag
names the enumerated type. (Refer to the section 'Enumeration' later in this chapter for
information on enumerated types.) Then you can use the tag with the struct or union
keyword to declare variables of that type without respecifying member declarations. A
structure tag occupies a separate name space reserved for tags. Thus, a structure tag may
have the same spelling as a structure member or an ordinary identifier. Structure tags also
obey the normal block scope associated with identifiers. Another tag of the same spelling in
a subordinate block may hide a structure tag in an outer block.
A struct or union declaration has two parts: the structure body, where the members of the
structure are declared (and possibly a tag name associated with them); and a list of
declarators (objects with the type of the structure).
Either part of the declaration can be empty. Thus, you can put the structure body
declaration in one place, and use the struct type in another place to declare objects of that
type. For example, consider the following declarations.
struct s1 {
int x;
float y;
};
struct s1 obj1, *obj2;
The first example declares only the struct body and its associated tag name. The second
example uses the struct tag to declare two objects — obj1 and obj2. They are, respectively,
a structure object of type "struct s1" and a pointer object, which point to an object type
"struct s1."
This allows you to separate all the struct body declarations into one place (for example, a
header file) and use the struct types elsewhere in the program when declaring objects.
Consider the following example:
struct examp {
float f; /* floating member */
int i; /* integer member */
}; /* no declaration list */
In this example, the structure tag is examp and it is associated with the structure body
that contains a single floating-point quantity and an integer quantity. Note that no objects
are declared after the definition of the structure's body; only the tag is being defined.
A subsequent declaration may use the defined structure tag:
struct examp x, y[100];
This example defines two objects using type struct
examp. The first is a single structure named x and the second, y, is an array of structures of
type struct examp.
Another use for structure tags is to write self-referential structures. A structure of type S