public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* About nested Designated Initializers
@ 2011-07-19 21:59 Cheng Renquan
  2011-07-19 23:08 ` Cheng Renquan
  0 siblings, 1 reply; 7+ messages in thread
From: Cheng Renquan @ 2011-07-19 21:59 UTC (permalink / raw)
  To: gcc

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 (程任全)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-07-20  9:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-19 21:59 About nested Designated Initializers Cheng Renquan
2011-07-19 23:08 ` Cheng Renquan
2011-07-19 23:45   ` Jonathan Wakely
2011-07-20  0:05     ` Cheng Renquan
2011-07-20  0:26       ` Jonathan Wakely
2011-07-20  6:53         ` Cheng Renquan
2011-07-20  9:43         ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).