public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Default "others" value for designated array initializers
@ 2019-12-04  1:01 Yonatan Goldschmidt
  0 siblings, 0 replies; only message in thread
From: Yonatan Goldschmidt @ 2019-12-04  1:01 UTC (permalink / raw)
  To: gcc

Hello GCC community,

Designated initializers can be used in arrays to initialize by index:

    int a[6] = { [4] = 29, [2] = 15 }

and by range:

    int a[6] = { [2 ... 4] = 5 }

but it seems there's no syntax to initialize all the "others" of the elements, i.e those not
specified by any initializer.

After a bit of playing around, I found that you can override previous initializers. So
it allows to make defaults this way:

    int a[6] = { [0 ... 5] = -1, [2 ... 4] = 5, [5] = 15 }

However, I find this method very error prone. Designated initializers, in my opinion, are supposed
to allow me to disregard the initialization order, because I have field names for structs/unions,
and indices for arrays, thus the order doesn't matter.
But when I use this trick, it requires me to mind this very first line, otherwise my array will
go terribly wrong.

Any reason GCC doesn't have a special syntax for this case?
I couldn't find anything about it online / in this list.

Thanks,

Yonatan

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-12-04  1:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-04  1:01 Default "others" value for designated array initializers Yonatan Goldschmidt

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).