HP C A.06.05 Reference Manual
Data Types and Declarations
Structure and Union Specifiers
Chapter 3 49
Members are placed in physical storage in the same order as they are declared in the
definition of the structure. A member's offset is the distance from the start of the structure to
the beginning of the member. The compiler inserts pad bytes as necessary to insure that
members are properly aligned. For example, if a char member is followed by a float member,
one or more pad bytes may be inserted to insure that the float member begins on an
appropriate boundary.
Unions are like structures except that all members of a union have a zero offset from the
beginning of the union. In other words, the members overlap. Unions are a way to store
different type of objects in the same memory location.
A declarator for a member of a structure or union may occupy a specified number of bits. This
is done by following the declarator with a colon and a constant non-negative integral
expression. The value of the expression indicates the number of bits to be used to hold the
member. This type of member is called a bit-field. Only integral type specifiers are allowed for
bit-field declarators.
In structures, bit-fields are placed into storage locations from the most significant bits to the
least significant bits. Bit-fields that follow one another are packed into the same storage
words, if possible. If a bit-field will not fit into the current storage location, it is put into the
beginning of the next location and the current location is padded with an unnamed field.
A colon followed by an integer constant expression indicates that the compiler should create
an unnamed bit-field at that location. In addition, a colon followed by a zero indicates that the
current location is full and that the next bit-field should begin at the start of the next storage
location.
Although bit-fields are permitted in unions (ANSI mode only), they are just like any other
members of the union in that they have a zero offset from the beginning of the union. That is,
they are not packed into the same word, as in the case of structures. The special cases of
unnamed bit-fields and unnamed bit-fields of length zero behave differently with unions; they
are simply unnamed members that cannot be assigned to.
The unary address operator (&) may not be applied to bit-fields. This implies that there cannot
be pointers to bit-fields nor can there be arrays of bit-fields.
Refer to Chapter 10, “HP C/HP-UX Implementation Topics,” on page 235 for more information
on bit-fields.
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 “Enumeration” on page 52 for information on
enumerated types.) Then you can use the tag with the struct or union keyword to declare
variables of that type without re-specifying member declarations. A structure tag occupies a