Structure and Union Specifiers [ HP C/iX Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Reference Manual
Structure and Union Specifiers
A structure specifier indicates an aggregate type consisting of a
sequence of named members. A union specifier defines a type whose
members begin at offset zero from the beginning of the union.
Syntax
struct-or-union specifier ::=
struct-or-union [identifier] { struct-declaration-list }
struct-or-union identifier
struct-or-union ::=
struct
union
struct-declaration-list ::=
struct-declaration
struct-declaration-list struct-declaration
struct-declaration ::=
specifier-qualifier-list struct-declarator-list;
specifier-qualifier-list ::=
type-specifier [specifier-qualifier-list]
type-qualifier [specifier-qualifier-list]
struct-declarator-list ::=
struct-declarator
struct-declarator-list , struct-declarator
struct-declarator ::=
declarator
[declarator] : constant-expression
Description
A structure is a named collection of members. Each member belongs to a
name space associated with the structure. Members in different
structures can have the same names but represent different objects.
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.
The HP C Programmer's Guide provides a detailed comparison of storage and
alignment on HP computers.
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. Refer to chapter 9 for the treatment of the sign for
bit-fields.
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.
The largest bit-field is the length of an int (refer to chapter 9 for
exact sizes). Bit-fields do not straddle a word boundary.
MPE/iX 5.0 Documentation