• Aucun résultat trouvé

Type Specifiers

Dans le document III CP·6 (Page 79-85)

Syntax:

type-.specifier:

void char short int long float double signed unsigned

.struct-or-union-.specifier enum-.specifier

typedef-name Constraints:

Data Declarations

Each list of type specifiers must be one of the following sets (delimited by commas, when there is more than one set on aline); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers:

• void

• char

• signed char

• unsigned char

• short, signed short, short int, or signed short int

• unsigned short or unsigned short int

• int, signed, signed int, or no type specifiers

• unsigned or unsigned int

• long, signed long, long int, or signed long int

• unsigned long or unsigned long int

• float

• double

• long double

• struct-or-union specifier

• enum-specifier

• typedef-name

HA17-00 Type Specifiers 5-3

Data Declarations Semantics:

Specifiers for structures, unions, and enumerations are discussed later in this section, as are declarations of typedef names. The characteristics of the other types are discussed in Section 2, Lexical Elements.

Each of the above comma-separated sets designates the same type. In some C compilers, the field type signed int (or signed) may differ from int (or no type specifiers).

Structure and Union Specifiers Syntax:

struct-or-union-specifier:

struct-or-union identifieropt { 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 s pecifier- qualifier-list:

type-specifier specifier-qualifier-listopt type-qualifier specifier-qualifier-listopt struct-declarator-list:

struct-declarator

struct-declarator-li.st , struct-declarator struct-declarator:

declarator declarator opt Constraints:

constant- expression

A structure or union may not contain a member with incomplete or function type. Hence it may not contain an instance of itself (but may contain a pointer to an instance of itself).

The expression that specifies the width of a bit-field is an integral constant expression that has non-negative value that may not exceed the number of bits in an ordinary object of compatible type. If the value is zero, the declaration may not have a declarator.

5-4 Structure and Union Specifiers HA17-00

Data Declarations Semantics:

As discussed in Section 2, Lexical Elements, a structure is a type consisting of a sequence of named members, whose storage is allocated in an ordered sequence, and a union is a type consisting of a sequence of named members, whose storage overlaps.

Structure and union specifiers have the same form.

The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within an object unit. The struct-declaration-list is a sequence of declarations for the members of the structure or union. The struct-declaration-list should contain at least one named member. The type is incomplete until after the} that terminates the list.

A member of a structure or union may have any object type. In addition, a member may that immediately follows another bit-field in a structure is packed into adjacent bits of the same unit. If insufficient space remains, a bit-field that does not fit is put into the next unit. The order of allocation of bit-fields within a unit is high-order to low-order. The addressable storage unit is word-aligned.

A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field.4 As a special case of this, a bit-field structure member with a width of

o

indicates that no further bit-field is to be packed into the unit in which the previous bit-field, if any, was placed.

Each non-bit-field member of a structure or union object is aligned according to its type.

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may therefore be unnamed holes within a structure object, but not at its beginning, as necessary to achieve the appropriate alignment.

The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.

There may also be unnamed padding at the end of a structure or union, as necessary to achieve the appropriate alignment were the structure or union to be an element of an array.

3 The unary i (address-of) operator may not be applied to a bit-field object; thus there are no pointers to or arrays of bit-field objects.

4 An unnamed bit-field structure member is useful for padding to conform to externally-imposed layouts.

HA17-00 Structure and Union Specifiers 5-5

Data Declarations enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with

=

may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.

Each enumerated type is compatible with type int.

Examples: other and from other identifiers declared in ordinary declarators.

5-6 Enumeration Specifiers HA17-00

Data Declarations Tags

Semantics:

A type specifier of the form

struct-or-union identifier { struct-declaration-list } or

anum identifier { enumerator-list }

declares the identifier to be the tag of the structure, union, or enumeration specified by the list. The list defines the structure content, union content, or enumeration content. If this declaration of the tag is visible, a subsequent declaration that uses the tag and that omits the bracketed list specifies the declared structure, union, or enumerated type. Subsequent declarations in the same scope must omit the bracketed list.

If a type specifier of the form struct-or-union identifier

occurs prior to the declaration that defines the content, the structure or union is an incomplete type.61t declares a tag that specifies a type that may be used only when the size of an object of the specified type is not needed.7If the type is to be completed, another declaration of the tag in the same scope (but not in an enclosed block, which declares a new type known only within that block) defines the content. A declaration of the form

struct-or-union identifier ;

specifies a structure or union type and declares a tag, both visible only within the scope in which the declaration occurs. It specifies a new type distinct from any type with the same tag in an enclosing scope (if any).

A type specifier of the form

struct-or-union { struct-declaration-list } or

anum { enumerator-list}

specifies a new structure, union, or enumerated type, within the object unit, that can only be referred to by the declaration of which it is a part.8

6 A similar construction with enum does not exist and is not necessary as there can be no mutual dependencies between the declaration of an enumerated type and any other type.

7 It is not needed, for example, when a typedef name is declared to be a specifier for a structure or union, or when a pointer to or a function returning a structure or union is being declared. (See incomplete types in Section 2, Lexical Elements.) The specification must be complete before such a function is called or defined.

8 Of course, when the declaration is of a typedef name, subsequent declarations can make use of the typedef name to declare objects having the specified structure, union, or enumerated type.

HA17-00 Tags 5-7

Data Declarations Examples:

This mechanism allows declaration of a self-referential structure:

struct tnode { int count;

struct tnode *left, *right;

};

specifies a structure that contains an integer and two pointers to objects of the same type.

Once this declaration has been given, the declaration struct tnode s, *sp;

declares s to be an object of the given type and sp to be a pointer to an object of the given type. With these declarations, the expression sp->left refers to the left struct tnode pointer of the object to which sp points; the expression s .right->count designates the count member of the right struct tnode pointed to from s.

The following alternative formulation uses the typedef mechanism:

typedef struct tnode INODE;

struct tnode { int count;

TNODE *left, *right;

};

TNODE s, *sp;

To illustrate the use of prior declaration of a tag to specify a pair of mutually-referential structures, the declarations

struct s1 { struct s2 *s2p; /* ... */ }; /* D1 */

struct s2 { struct s1 *s1p; /* ... */ }; /* D2 */

specify a pair of structures that contain pointers to each other. Note, however, that if s2 were already declared as a tag in an enclosing scope, the declaration D1 would refer to

it,

not to the tag s2 declared in D2. To eliminate this context sensitivity, the otherwise vacuous declaration

struct s2;

may be inserted ahead of D1. This declares a new tag s2 in the inner scope; the declaration D2 then completes the specification of the new type.

5-8 Tags HA17-00

Dans le document III CP·6 (Page 79-85)