top | item 9044851

(no title)

matthavener | 11 years 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)

discuss

order

andolanra|11 years ago

I 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.

yadyad|11 years ago

How does this even work?!?!