Default "others" value for designated array initializers
Yonatan Goldschmidt
yon.goldschmidt@gmail.com
Wed Dec 4 01:01:00 GMT 2019
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
More information about the Gcc
mailing list