什么是 char c[2] = { [1] = 7 };做?

What does char c[2] = { [1] = 7 }; do?

我正在阅读Bruce Dawson's article on porting Chromium to VC 2015, and he encountered some C code that I don't understand

密码是:

char c[2] = { [1] = 7 };

Bruce 对此的唯一评论是:"I am not familiar with the array initialization syntax used - I assume it is some C-only construct."那么这个语法到底是什么意思?

C99 允许您以任何顺序指定数组的元素(如果您正在搜索它,这似乎被称为 "Designated Initializers" )。因此,此构造将 7 分配给 c.

的第二个元素

此表达式等同于 char c[2] = {0, 7};,它不会为如此短的初始化程序保存 space,但对较大的稀疏数组非常有帮助。

查看此页面了解更多信息: https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

它的意思是

char c[2]={ 0, 7 }

或者你可以说

char c[2];
c[0]=0;
c[1]=7;