This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Default "others" value for designated array initializers


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]