简体   繁体   English

这是数组文字中的语法 - [x] = 'y' - 标准 C?

[英]Is this syntax - [x] = ‘y’ in an array literal - standard C?

const char Table[256] = {
     ['a'] = 'a', ['A'] = 'a',
     ['b'] = 'b', ['B'] = 'b',
     ['c'] = 'c', ['C'] = 'c',
     ['d'] = 'd', ['D'] = 'd',
     ['e'] = 'e', ['E'] = 'e',
     ['f'] = 'f', ['f'] = 'f',
     ['g'] = 'g', ['g'] = 'g',
     ['h'] = 'h', ['H'] = 'h',
     ['i'] = 'i', ['I'] = 'i',
     ['j'] = 'j', ['J'] = 'j',
     /* Etc */
};

I have seen something like that in an answer to a C question.我在 C 问题的回答中看到了类似的内容。 Is this Lua-like syntax standard C, or is it some compiler extension?这是类似 Lua 的语法标准 C,还是一些编译器扩展?

Yes, that syntax is standard since the C99 (the 1999 ISO C standard).是的,自 C99 以来,该语法是标准的(1999 ISO C 标准)。

It's described in the C standard: https://port70.net/~nsz/c/c11/n1570.html#6.7.9在 C 标准中有描述: https://port70.net/~nsz/c/c11/n1570.html#6.7.9

(The link is to an HTML version of the N1570 draft, which is very close to the published 2011 edition.) (该链接指向 N1570 草案的 HTML 版本,与 2011 年发布的版本非常接近。)

Designated initializers can also be used for struct and union members, with the syntax .name = value rather than [index] = value .指定的初始化器也可用于结构和联合成员,语法为.name = value而不是[index] = value

Designated initializers can also be used in compound literals : https://port70.net/~nsz/c/c11/n1570.html#6.5.2.5指定的初始值设定项也可用于复合文字https://port70.net/~nsz/c/c11/n1570.html#6.5.2.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM