c - 这在c中是什么意思?大括号内的方括号

标签 c

<分区>

const int status[STATUS_SIZE] = {
        [0] = -1, 
        [1] = 0,
        [2] = 1,
};

const char *messages[MESSAGE_SIZE] = {
        [0] = "OK",
        [1] = "NG",
};

你能解释一下吗?

最佳答案

C99 介绍 Designated Initializers ,你可以用它来初始化一个数组 以任何顺序使用索引。

6.27 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/

相关文章:

C - 外部 ELF 加载

c - 从函数返回多个错误之一并用C结束程序的最佳实践是什么?

c - Valgrind 内存泄漏报告中的时间戳不正确

c - 包含 strptime() 函数声明的问题

C 结构体到 Delphi 记录(转换)

使用带有 gcc 和自排序列表的 .h 文件进行编译

c - 使用带有 while 循环的 scanf 输入两个单独的字符串

c++ - 客户端到客户端套接字通信

C Socket,在数据流之前添加 header

c - Linux/C : Get ip address from device name?