Стандарт языка Си С99 TC (1113411), страница 28
Текст из файла (страница 28)
The type of distance is int, that of metricp is ‘‘pointer to function with noparameter specification returning int’’, and that of x and z is the specified structure; zp is a pointer tosuch a structure. The object distance has a type compatible with any other int object.5EXAMPLE 2After the declarationstypedef struct s1 { int x; } t1, *tp1;typedef struct s2 { int x; } t2, *tp2;type t1 and the type pointed to by tp1 are compatible.
Type t1 is also compatible with type structs1, but not compatible with the types struct s2, t2, the type pointed to by tp2, or int.§6.7.7Language123ISO/IEC 9899:TC36EXAMPLE 3Committee Draft — Septermber 7, 2007WG14/N1256The following obscure constructionstypedef signed int t;typedef int plain;struct tag {unsigned t:4;const t:5;plain r:5;};declare a typedef name t with type signed int, a typedef name plain with type int, and a structurewith three bit-field members, one named t that contains values in the range [0, 15], an unnamed constqualified bit-field which (if it could be accessed) would contain values in either the range [−15, +15] or[−16, +15], and one named r that contains values in one of the ranges [0, 31], [−15, +15], or [−16, +15].(The choice of range is implementation-defined.) The first two bit-field declarations differ in thatunsigned is a type specifier (which forces t to be the name of a structure member), while const is atype qualifier (which modifies t which is still visible as a typedef name).
If these declarations are followedin an inner scope byt f(t (t));long t;then a function f is declared with type ‘‘function returning signed int with one unnamed parameterwith type pointer to function returning signed int with one unnamed parameter with type signedint’’, and an identifier t with type long int.7EXAMPLE 4 On the other hand, typedef names can be used to improve code readability.
All three of thefollowing declarations of the signal function specify exactly the same type, the first without making useof any typedef names.typedef void fv(int), (*pfv)(int);void (*signal(int, void (*)(int)))(int);fv *signal(int, fv *);pfv signal(int, pfv);8EXAMPLE 5 If a typedef name denotes a variable length array type, the length of the array is fixed at thetime the typedef name is defined, not each time it is used:void copyt(int n){typedef int B[n];//n += 1;B a;//int b[n];//for (int i = 1; i < n;a[i-1] = b[i];}124B is n ints, n evaluated nowa is n ints, n without += 1a and b are different sizesi++)Language§6.7.7WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC36.7.8 InitializationSyntax1initializer:assignment-expression{ initializer-list }{ initializer-list , }initializer-list:designationopt initializerinitializer-list , designationopt initializerdesignation:designator-list =designator-list:designatordesignator-list designatordesignator:[ constant-expression ].
identifierConstraints2No initializer shall attempt to provide a value for an object not contained within the entitybeing initialized.3The type of the entity to be initialized shall be an array of unknown size or an object typethat is not a variable length array type.4All the expressions in an initializer for an object that has static storage duration shall beconstant expressions or string literals.5If the declaration of an identifier has block scope, and the identifier has external orinternal linkage, the declaration shall have no initializer for the identifier.6If a designator has the form[ constant-expression ]then the current object (defined below) shall have array type and the expression shall bean integer constant expression. If the array is of unknown size, any nonnegative value isvalid.7If a designator has the form. identifierthen the current object (defined below) shall have structure or union type and theidentifier shall be the name of a member of that type.§6.7.8Language125ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256Semantics8An initializer specifies the initial value stored in an object.9Except where explicitly stated otherwise, for the purposes of this subclause unnamedmembers of objects of structure and union type do not participate in initialization.Unnamed members of structure objects have indeterminate value even after initialization.10If an object that has automatic storage duration is not initialized explicitly, its value isindeterminate.
If an object that has static storage duration is not initialized explicitly,then:— if it has pointer type, it is initialized to a null pointer;— if it has arithmetic type, it is initialized to (positive or unsigned) zero;— if it is an aggregate, every member is initialized (recursively) according to these rules;— if it is a union, the first named member is initialized (recursively) according to theserules.11The initializer for a scalar shall be a single expression, optionally enclosed in braces. Theinitial value of the object is that of the expression (after conversion); the same typeconstraints and conversions as for simple assignment apply, taking the type of the scalarto be the unqualified version of its declared type.12The rest of this subclause deals with initializers for objects that have aggregate or uniontype.13The initializer for a structure or union object that has automatic storage duration shall beeither an initializer list as described below, or a single expression that has compatiblestructure or union type.
In the latter case, the initial value of the object, includingunnamed members, is that of the expression.14An array of character type may be initialized by a character string literal, optionallyenclosed in braces. Successive characters of the character string literal (including theterminating null character if there is room or if the array is of unknown size) initialize theelements of the array.15An array with element type compatible with wchar_t may be initialized by a widestring literal, optionally enclosed in braces. Successive wide characters of the wide stringliteral (including the terminating null wide character if there is room or if the array is ofunknown size) initialize the elements of the array.16Otherwise, the initializer for an object that has aggregate or union type shall be a braceenclosed list of initializers for the elements or named members.17Each brace-enclosed initializer list has an associated current object.
When nodesignations are present, subobjects of the current object are initialized in order accordingto the type of the current object: array elements in increasing subscript order, structure126Language§6.7.8WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3members in declaration order, and the first named member of a union.129) In contrast, adesignation causes the following initializer to begin initialization of the subobjectdescribed by the designator. Initialization then continues forward in order, beginningwith the next subobject after that described by the designator.130)18Each designator list begins its description with the current object associated with theclosest surrounding brace pair. Each item in the designator list (in order) specifies aparticular member of its current object and changes the current object for the nextdesignator (if any) to be that member.131) The current object that results at the end of thedesignator list is the subobject to be initialized by the following initializer.19The initialization shall occur in initializer list order, each initializer provided for aparticular subobject overriding any previously listed initializer for the same subobject;132)all subobjects that are not initialized explicitly shall be initialized implicitly the same asobjects that have static storage duration.20If the aggregate or union contains elements or members that are aggregates or unions,these rules apply recursively to the subaggregates or contained unions.
If the initializer ofa subaggregate or contained union begins with a left brace, the initializers enclosed bythat brace and its matching right brace initialize the elements or members of thesubaggregate or the contained union. Otherwise, only enough initializers from the list aretaken to account for the elements or members of the subaggregate or the first member ofthe contained union; any remaining initializers are left to initialize the next element ormember of the aggregate of which the current subaggregate or contained union is a part.21If there are fewer initializers in a brace-enclosed list than there are elements or membersof an aggregate, or fewer characters in a string literal used to initialize an array of knownsize than there are elements in the array, the remainder of the aggregate shall beinitialized implicitly the same as objects that have static storage duration.22If an array of unknown size is initialized, its size is determined by the largest indexedelement with an explicit initializer.
At the end of its initializer list, the array no longerhas incomplete type.129) If the initializer list for a subaggregate or contained union does not begin with a left brace, itssubobjects are initialized as usual, but the subaggregate or contained union does not become thecurrent object: current objects are associated only with brace-enclosed initializer lists.130) After a union member is initialized, the next object is not the next member of the union; instead, it isthe next subobject of an object containing the union.131) Thus, a designator can only specify a strict subobject of the aggregate or union that is associated withthe surrounding brace pair.
Note, too, that each separate designator list is independent.132) Any initializer for the subobject which is overridden and so not used to initialize that subobject mightnot be evaluated at all.§6.7.8Language127ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N125623The order in which any side effects occur among the initialization list expressions isunspecified.133)24EXAMPLE 1Provided that <complex.h> has been #included, the declarationsint i = 3.5;double complex c = 5 + 3 * I;define and initialize i with the value 3 and c with the value 5.
0 + i3. 0.25EXAMPLE 2The declarationint x[] = { 1, 3, 5 };defines and initializes x as a one-dimensional array object that has three elements, as no size was specifiedand there are three initializers.26EXAMPLE 3The declarationint y[4][3] ={ 1, 3,{ 2, 4,{ 3, 5,};{5 },6 },7 },is a definition with a fully bracketed initialization: 1, 3, and 5 initialize the first row of y (the array objecty[0]), namely y[0][0], y[0][1], and y[0][2]. Likewise the next two lines initialize y[1] andy[2]. The initializer ends early, so y[3] is initialized with zeros.