What do square brackets mean in array initialization in C?

后端 未结 4 756
你的背包
你的背包 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:03

    It means initialise the n-th element of the array. The example you've given will mean that:

    togglecode[0x3A] == CAPSLOCK
    togglecode[0x45] == NUMLOCK
    togglecode[0x46] == SCROLLLOCK
    

    These are called "designated initializers", and are actually part of the C99 standard. However, the syntax without the = is not. From that page:

    An alternative syntax for this which has been obsolete since GCC 2.5 but GCC still accepts is to write [index] before the element value, with no =.

提交回复
热议问题