|
|
Scalar types with static or automatic storage duration are initialized with a single expression, optionally enclosed in braces. Example:
int i = 1;Additionally, scalar types (with automatic storage duration only) may be initialized with a nonconstant expression.
An initializer for a union with static storage duration must be enclosed in braces, and it initializes the first member in the declaration list of the union. The initializer must have a type that can be converted to the type of the first union member. Example:
union { int i; float f; } u = {1}; / initialize u.i /For a union with automatic storage duration, if the initializer is enclosed in braces, it must consist of constant expressions that initialize the first member of the union. If the initializer is not enclosed in braces, it must be an expression that has the matching union type.
The members of a structure may be initialized by initializers that can be converted to the type of the corresponding member.
struct s { int i; char c; char s; } st = { 3, 'a', "abc" };This example illustrates initialization of all three members of the structure. If initialization values are missing, as in
struct s st2 = {5}
;
then the first member is initialized
(in this case, member i
is initialized with a value of ``5''),
and any uninitialized member is initialized with ``0''
for arithmetic types and a null pointer constant for pointer types.
For a structure with automatic storage duration, if the initializer is enclosed in braces, it must consist of constant expressions that initialize the respective members of the structure. If the initializer is not enclosed in braces, it must be an expression that has the matching structure type.
The number of initializers for an array must not exceed the dimension (the declared number of elements), but there may be fewer initializers than the number of elements. When the number of initializers is less than the size of the array, the first array elements are initialized with the values given, until the supply of initializers is exhausted. Any remaining array elements are initialized with the value 0 or a null pointer constant, as explained above in the discussion of structures. Example:
int ia[5] = { 1, 2 };In this example, an array of five ints is declared, but only the first two members are initialized explicitly. The first member, ia[0], is initialized with a value of ``1''; the second member, ia[1], is initialized with a value of ``2''. The remaining members are initialized with a value of ``0''.
When no dimensions are given, the array is sized to hold exactly the number of initializers supplied.
A character array may be initialized with a string literal, as in:
char ca[] = { "abc" }; /curly braces are optional/where the size of the array is four (three characters with a null byte appended). The following:
char cb[3] = "abc";is valid; however, in this case the null byte is discarded. But:
char cc[2] = "abc";is erroneous because there are more initializers than the array can hold.
Arrays may be initialized similarly with wide characters:
wchar_t wc[] = L"abc";Initializing subaggregates (for example, arrays of arrays) requires the proper placement of braces. For example:
int ia [4][2] = { 1, 2, 3, 4 };initializes the first two rows of ia (ia[0][0], ia[0][1], ia[1][0], and ia[1][1]), and initializes the rest to ``0''. This is a ``minimally bracketed'' initialization.
Note that a similar ``fully bracketed'' initialization yields a different result:
int ia [4][2] = { {1}, {2}, {3}, {4}, };initializes the first column of ia (ia[0][0], ia[1][0], ia[2][0], and ia[3][0]), and initializes the rest to ``0''.
Mixing the fully and minimally bracketed styles may lead to unexpected results. Use one style or the other consistently.