简体   繁体   English

方括号在C中的数组初始化中意味着什么?

[英]What do square brackets mean in array initialization in C?

static uint8_t togglecode[256] = {
    [0x3A] CAPSLOCK,
    [0x45] NUMLOCK,
    [0x46] SCROLLLOCK
};

What's the meaning of [0x3A] here? [0x3A]在这里是什么意思? I have only learned statements like int a[2] = {1, 2}; 我只学过类似int a[2] = {1, 2};语句int a[2] = {1, 2};

It means initialise the n -th element of the array. 这意味着初始化数组的第n个元素。 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. 这些称为“指定的初始化程序” ,实际上是C99标准的一部分。 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 = . 自GCC 2.5起已经过时但GCC仍然接受的对此的另一种语法是在元素值之前写入[index] ,而no =

According to the GCC docs this is ISO C99 compliant. 根据GCC文档,这符合ISO C99。 They refer to it as "Designated Initialzers": 他们将其称为“指定的初始化器”:

To specify an array index, write `[index] =' before the element value. 要指定数组索引,请在元素值前写“ [index] =”。 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. 我以前从未见过这种语法,但是我只是使用gcc 4.4.5和-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. 从该示例可以看到,它允许您初始化特定的数组元素,而其他元素则保持不变。

That was introduced in C99 and it's called a designated initialiser . 这是C99中引入的,称为指定的初始化程序

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 . 0x3a设置#1 (请参见10.6节)中CapsLock键的扫描代码0x45NumLock ,0x46是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. 有趣的是,尽管该链接指出=是必需的,但在这里似乎并非如此。

It's (close to) the syntax of designated initializers , a C99 feature. 它(接近) 指定的初始化程序的语法(一种C99功能)。

Basically, it initializes parts of an array, for example; 例如,基本上,它初始化数组的一部分;

int aa[4] = { [2] = 3, [1] = 6 };

Intializes the second value of the array to 6, and the third to 3. 将数组的第二个值初始化为6,将第三个值初始化为3。

In your case the array offsets happen to be in hex (0x3a) which initializes the 58'th element of the array to the value of CAPSLOCK which presumably is defined in the code above the code you're showing. 在您的情况下,数组偏移恰好是十六进制(0x3a),它将数组的第58个元素初始化为CAPSLOCK的值,该值大概在您显示的代码上方的代码中定义。

The version in your code without the = seems to be a gcc specific extension. 您代码中不带=的版本似乎是gcc特定的扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM