方括号在 C 中的数组初始化中意味着什么? [英] What do square brackets mean in array initialization in C?

查看:35
本文介绍了方括号在 C 中的数组初始化中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

What's the meaning of [0x3A] here? I have only learned statements like int a[2] = {1, 2};

推荐答案

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

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

这些被称为指定的初始值设定项",实际上是 C99 标准的一部分.但是,没有 = 的语法不是.从该页面:

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

自 GCC 2.5 以来已经过时但 GCC 仍然接受的另一种语法是在元素值之前编写 [index],没有 =.

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 中的数组初始化中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆