This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
About nested Designated Initializers
- From: Cheng Renquan <crquan at gmail dot com>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 19 Jul 2011 14:56:20 -0700
- Subject: About nested Designated Initializers
Hi all,
From info gcc I know it accepts a series of `.FIELDNAME' and `[INDEX]'
designators,
like
struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
But in my case, I have a struct with array of int as members,
struct mbox {
int x[20];
int y[20];
};
and want to declare a mbox variable with partially initialized, like
struct mbox mbox = { .x = { 1, 2 }, .y[19] = 3, };
During compiling, I see warnings, and the compiled program doesn't
work like what I think,
slfe.c:823: warning: braces around scalar initializer
slfe.c:823: warning: (near initialization for `mbox.s[2]')
slfe.c:825: warning: passing arg 1 of `hexdump' from incompatible pointer type
Then I use a hexdump on this var, found it's working like
struct mbox mbox = { .x = { 1, 2 }, .y = 3, };
I wonder if this is gcc bug, or if gcc doesn't support this kind of
designated initializer,
But if this is supported,
struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
Why can't we support this?
struct mbox mbox = { .x = { 1, 2 }, .y[19] = 3, };
Or someone knows which standard (like C99?) has related spec?
Thanks,
5.20 Designated Initializers
============================
Standard C89 requires the elements of an initializer to appear in a
fixed order, the same as the order of the elements in the array or
structure being initialized.
You can also write a series of `.FIELDNAME' and `[INDEX]' designators
before an `=' to specify a nested subobject to initialize; the list is
taken relative to the subobject corresponding to the closest
surrounding brace pair. For example, with the `struct point'
declaration above:
struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
--
Cheng Renquan (çäå)