C数组的范围初始化 [英] Range Initialization of a C Array
问题描述
很简单的问题,该代码在在哪里起作用?
Very simple question of where does this code work?
static void *gostruct[] =
{
[0 ... 255] = &&l_bad,
['\t'] = &&l_loop, [' '] = &&l_loop, ['\r'] = &&l_loop, ['\n'] = &&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.
推荐答案
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屋!