Section 2: Compiler
89
TI
-
89 / TI
-
92 Plus Sierra C Assembler Reference Manual
Not for Distribution
Beta Version February 2, 2001
The braces surrounding subaggregate initializers are not always necessary.
When a subaggregate initializer does not begin with a brace, only enough
initializers are taken from the list to account for the members of the
subaggregate. If there are too few initializers in the list to initialize the
subaggregate, the remainder of the subaggregate is padded with zeros. If there
are more initializers in the list than needed to initialize the subaggregate, the
remainder of the initializers in the list initialize the next member of the aggregate
of which the subaggregate is a member. For example, the following two
declarations are equivalent:
short x[2][3]
=
{ {1, 2, 3}, {4, 5, 6} };
short x[2][3]
=
{1, 2, 3, 4, 5, 6};
The following two examples, however, are not equivalent:
short x[2][3]
=
{1, 2};
short x[2][3]
=
{{1}, 2};
The first defines an array in which x[0][0] = 1, x[0][1] = 2, and all other elements
are zero; the second defines in array in which x[0][0] = 1, x[1][0] = 2, and all other
elements are zero. The following declaration is illegal:
short x[2][3]
=
{1, {2,3}};
The declaration initialization list can also be used to establish the size of an
array. In the following example, the array is determined to have four elements:
double z[]
=
{1.2, 2.3, 4.555, 3.14};
Finally, a character array may be initialized by a character string, optionally
enclosed in braces. Successive characters from the character string — including
the terminating null character if there is room or if no size has been specified for
the array — are used to initialize the array. The following example illustrates
equivalent methods for declaring arrays s and t:
char s[3]
=
"abc";
char s[]
=
{'a', 'b', 'c'};
char s[] = {"a"
"b"
"c"
};
char t[]
=
"xyz";
char t[]
=
{'x', 'y', 'z', '\0'};
The following two declarations are similar, but not identical:
char
*
p
=
"abc";
char q[]
=
"abc";