What do square brackets mean in array initialization in C?

后端 未结 4 757
你的背包
你的背包 2020-11-30 23:14
static uint8_t togglecode[256] = {
    [0x3A] CAPSLOCK,
    [0x45] NUMLOCK,
    [0x46] SCROLLLOCK
};

What\'s the meaning of [0x3A] her

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 23:47

    According to the GCC docs this is ISO C99 compliant. They refer to it as "Designated Initialzers":

    To specify an array index, write `[index] =' before the element value. For example,

     int a[6] = { [4] = 29, [2] = 15 };
    

    is equivalent to

     int a[6] = { 0, 0, 15, 0, 29, 0 };
    

    I've never seen this syntax before, but I just compiled it with gcc 4.4.5, with -Wall. It compiled successfully and gave no warnings.

    As you can see from that example, it allows you to initialize specific array elements, leaving the others untouched.

提交回复
热议问题