<分区>
const int status[STATUS_SIZE] = {
[0] = -1,
[1] = 0,
[2] = 1,
};
和
const char *messages[MESSAGE_SIZE] = {
[0] = "OK",
[1] = "NG",
};
你能解释一下吗?
标签 c
<分区>
const int status[STATUS_SIZE] = {
[0] = -1,
[1] = 0,
[2] = 1,
};
和
const char *messages[MESSAGE_SIZE] = {
[0] = "OK",
[1] = "NG",
};
你能解释一下吗?
最佳答案
C99 介绍 Designated Initializers ,你可以用它来初始化一个数组 以任何顺序使用索引。
Standard C90 requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized.
In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C90 mode as well. This extension is not implemented in GNU C++.
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 };
关于c - 这在c中是什么意思?大括号内的方括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48332693/