Designated Initializers
Designated initializers are a neat feature in C99 that I’ve used for about 6 years. I can’t fathom why anyone would not use them if C99 is available. (Of course if you have to support pre-C99 compilers, you’re very sad.) In case you’ve never seen them, consider this example that’s perfectly valid C99:
int abc[7] = { [1] = 0xabc, [2] = 0x12345678, [3] = 0x12345678, [4] = 0x12345678, [5] = 0xdef, };
As you may have guessed, indices 1–5 will have the specified value. Indices 0 and 6 will be zero. Cool, eh?
GCC Extensions
Today I learned about a neat GNU extension in GCC to designated initializers. Consider this code snippet:
int abc[7] = { [1] = 0xabc, [2 ... 5] = 0x12345678, [5] = 0xdef, };
Mind blowing, isn’t it?
Beware, however… GCC’s -std=c99 will not error out if you use ranges! You need to throw in -pedantic to get a warning.
$ gcc -c -Wall -std=c99 test.c $ gcc -c -Wall -pedantic -std=c99 test.c test.c:2:5: warning: ISO C forbids specifying range of elements to initialize [-pedantic]