c - Strange array initialize expression? -
what meaning of following code? code regression test suite of gcc.
static char * name[] = { [0x80000000] = "bar" };
in c99 can specify array indices assigned value, example:
static char * name[] = { [3] = "bar" };
is same as:
static char * name[] = { null, null, null, "bar"};
the size of array four. check example code working @ ideaone. in code array size 0x80000001
(its hexadecimal number).
note: uninitialized elements initialized 0
.
5.20 designated initializers:
in iso c99 can give elements in order, specifying array indices or structure field names apply to, , gnu c allows extension in c89 mode well. extension not implemented in gnu c++. specify array index, write
[index] =
before element value. example,int a[6] = { [4] = 29, [2] = 15 };
is equivalent to
int a[6] = { 0, 0, 15, 0, 29, 0 };
one more interesting declaration possible in gnu extension:
an alternative syntax has been obsolete since gcc 2.5 gcc still accepts write
[index]
before element value, no=
.to initialize range of elements same value, write
[first ... last] = value
. example,int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
note: length of array highest value specified plus one.
additionally, can combine technique of naming elements ordinary c initialization of successive elements. each initializer element not have designator applies next consecutive element of array or structure. example:
int a[6] = { [1] = v1, v2, [4] = v4 };
is equivalent to
int a[6] = { 0, v1, v2, 0, v4, 0 };
labeling elements of array initializer useful when indices characters or belong enum type. example:
int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\h'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
Comments
Post a Comment