HP 3000 Manuals

Structure and Union Tags [ HP C/iX Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Reference Manual

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 may contain a pointer to a structure of type S as
one of its members.  Note that a structure can never have itself as a
member because the definition of the structure's content would be
recursive.  A pointer to a structure is of fixed size, so it may be a
member.  Structures that contain pointers to themselves are key to most
interesting data structures.  For example, the following is the
definition of a structure that is the node of a binary tree:

     struct node {
        float data;            /* data stored at the node */
        struct node *left;     /* left subtree */
        struct node *right;    /* right subtree */
      };

This example defines the shape of a node type of structure.  Note that
the definition contains two members (left and right) that are themselves
pointers to structures of type node.

The C programming rule that all objects must be defined before use is
relaxed somewhat for structure tags.  A structure can contain a member
that is a pointer to an as yet undefined structure.  This allows for
mutually referential structures:

     struct s1 { struct s2 *s2p; };
     struct s2 { struct s1 *s1p; };

In this example, structure s1 references the structure tag s2.  When s1
is declared, s2 is undefined.  This is valid.

Example 

         struct tag1 {
             int m1;
             int   :16;  /* unnamed bit-field */
             int m2:16;  /* named bit-field; packed into */
                         /* same word as previous member */
             int m3, m4;
         };              /* empty declarator list */

         union tag2 {
             int u1;
             int   :16;
             int u2:16;  /* bit-field, starts at offset 0 */
             int u3, u4;
         } fudge1, fudge2; /* declarators denoting objects of the */
                           /* union type */

         struct tag1 obj1, *obj2;  /* use of type "struct tag1",   */
                                   /* whose body has been declared */
                                   /* above */



MPE/iX 5.0 Documentation