88
Section 2: Compiler
TI
-
89 / TI
-
92 Plus Sierra C Assembler Reference Manual
Not for Distribution
Beta Version February 2, 2001
When the initialized variable is an aggregate (structure or array), the initializer
consists of a brace-enclosed list of comma-separated initializers. The initializers
in the list are applied to the aggregate in increasing member or subscript order. If
the aggregate contains members that are also aggregates, the same rules apply
recursively to the subaggregates. If there are fewer initializers in a
brace-enclosed list than there are members in the aggregate, the remaining
members are initialized to the value zero. It is illegal for there to be more
initializers in a brace-enclosed list than there are members in the aggregate. For
initialization purposes a union is treated as a structure that contains a single
member, where the single member is the first member of the union.
The following is a fully initialized array of structures:
struct {
int a;
int b[3];
} c[2] = { {1, {2,3,4}}, {5, {6,7,8}} };
The initial values for the array c are as follows:
c[0].a
=
1, c[0].b[0]
=
2, c[0].b[1]
=
3, c[0].b[2]
=
4
c[1].a
=
5, c[1].b[0]
=
6, c[1].b[1]
=
7, c[1].b[2]
=
8
The following is a partially initialized array of structures:
struct {
int a;
int b[3];
} d[2] = { {1, {2}}, {5, {6,7}} };
The initial values for the array d are as follows:
d[0].a
=
1, d[0].b[0]
=
2, d[0].b[1]
=
0, d[0].b[2]
=
0
d[1].a
=
5, d[1].b[0]
=
6, d[1].b[1]
=
7, d[1].b[2]
=
0
Note:
When an aggregate is initialized, its scalar members can also be brace enclosed. The
following two declarations are equivalent:
int a[3] = { 10, 20, 30 };
int a[3] = { {10}, {20}, {30} };
The scalar-level braces are rarely used, however.