What do square brackets mean in array initialization in C?

后端 未结 4 759
你的背包
你的背包 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-12-01 00:11

    That was introduced in C99 and it's called a designated initialiser.

    It basically allows you to set specific values in an array with the rest left as defaults.

    In this particular case, the array indexes are the keyboard scan codes. 0x3a is the scan code in set #1 (see section 10.6) for the CapsLock key, 0x45 is NumLock and 0x46 is ScrollLock.

    On the first link above, it states that:

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

    is equivalent to:

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

    Interestingly enough, though the link states that = is necessary, that doesn't appear to be the case here.

提交回复
热议问题