C 数组的范围初始化 [英] Range Initialization of a C Array
问题描述
非常简单的问题,在哪里这段代码在哪里工作?
Very simple question of where does this code work?
static void *gostruct[] =
{
[0 ... 255] = &&l_bad,
[' '] = &&l_loop, [' '] = &&l_loop, ['
'] = &&l_loop, ['
'] = &&l_loop,
['"'] = &&l_qup,
[':'] = &&l_loop,[','] = &&l_loop,
['['] = &&l_up, [']'] = &&l_down, // tracking [] and {} individually would allow fuller validation but is really messy
['{'] = &&l_up, ['}'] = &&l_down,
['-'] = &&l_bare, [48 ... 57] = &&l_bare, // 0-9
['t'] = &&l_bare, ['f'] = &&l_bare, ['n'] = &&l_bare // true, false, null
};
通读它可以清楚地看到它将包含 256 个条目的数组初始化为值 &&l_bad,然后用特定值覆盖某些索引.但是这段代码不能在我可以访问的 VS2010 中编译,所以我想知道这是有效的 C 代码.
Reading through it its clear to see that it initializes an array containing 256 entries to the value &&l_bad and then overrides certain indexes with specific values. But this code does not compile in VS2010 which is what I have access to so I am wondering where this is valid C code.
注意:此代码片段取自 github 上的 JSON 解析器 据我了解,它创建了用于处理 JSON 字符串的跳转表.
NOTE: This code snippet was taken from a JSON parser on github that, from my understanding, creates jump tables for the processing of JSON strings.
推荐答案
这个结构叫做 指定初始化器.
在指定初始化器中使用 Range 是 GNU gcc 特定的扩展.
This construct is called Designated Initializers.
Using Range in Designated Initializers is a GNU gcc specific extension.
要将一系列元素初始化为相同的值,请编写 [first ... last] = value
.这是一个 GNU 扩展.例如,
To initialize a range of elements to the same value, write
[first ... last] = value
. This is a GNU extension. For example,
int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
用 -pedantic
编译它会告诉你.
请注意,它是不可移植的,因为它是特定于编译器的扩展.
Compiling it with -pedantic
shall tell you so.
Note that it is non-portable since it is a compiler specific extension.
这篇关于C 数组的范围初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!