6 ms·
My favorite C99 trick: const char *lookup[] = { [0] = "ZERO", [1] = "ONE", [4] = "FOUR" }; assert(strcmp(lookup[0], "ZERO") == 0)
by matthavener 12y ago
My favorite C99 trick:
const char *lookup[] = {
[0] = "ZERO",
[1] = "ONE",
[4] = "FOUR"
};
assert(strcmp(lookup[0], "ZERO") == 0);
(not available in C++ or pre-C99)
- andolanra 12y agoI see this used a lot with enums to make a kind of 'homogeneous struct' where all the members have the same type: enum fields = { x, y, z }; int point[] = { [x] = 1, [y] = 2, [z] = 3 }; This is used pretty extensively in the QEMU codebase, which is where I first learned it.