c - C 中数组初始化中的方括号是什么意思?

标签 c arrays

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

这里的[0x3A]是什么意思?我只学过像 int a[2] = {1, 2};

这样的语句

最佳答案

这意味着初始化数组的第n个元素。您给出的示例意味着:

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

这些被称为 "designated initializers" ,实际上是 C99 标准的一部分。但是,没有 = 的语法则不然。从该页面:

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 =.

关于c - C 中数组初始化中的方括号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43873929/

相关文章:

c++ - Xcode 中基于文件类型的奇怪语法错误

php - 读取二维数组的key

python - 从第三个数组中的两个数组中有效地获取每对元素的最小值

arrays - BigQuery : extract keys from json object, 将 json 从对象转换为键值数组

PHP:递归修改数组?

javascript - HTML a href 属性中的 node.js 变量

c - 如何高效地找到GTree中的最后一个键和值

c - 从文本文件创建二维数组

C lib直接从文件流中读取和解析JSON

c - 处理与消息队列和信号的同步