怪阵初始化前pression? [英] Strange array initialize expression?
问题描述
什么是以下code的含义是什么? code是从回归测试套件GCC的。
静态的char *名称[] = {
[0x80000000的] =栏
};
在C99可以指定数组下标分配的值,例如:
静态的char *名称[] = {
[3] =栏
};
是一样的:
静态的char *名称[] = {NULL,NULL,NULL,酒吧};
阵列的大小为四。检查的例子code。在 ideaone 工作。在您的code数组大小 0x80000001
(它的一个十六进制数)。结果
注: 0
初始化未初始化的元素。
5.20指定初始化的:
在ISO C99,你可以给任何顺序的元素,指定它们适用于数组索引和结构字段名,和GNU C允许本作中,C89模式扩展为好。这个扩展没有在GNU C ++实现的。
要指定数组索引,写[指数] =
元素值之前。例如,int类型的[6] = {[4] = 29,[2] = 15};
等同于
int类型的[6] = {0,0,15,0,29,0};
块引用>一个更有趣的声明有可能在GNU扩展:
这一个可选的语法,因为GCC 2.5这已经过时了,但GCC仍然接受是写
[指数]
元素值,没有=
。
要初始化一个范围的元素为相同的值,写
[第一...最后] =值
。例如,INT宽度[] = {[0 ... 9] = 1,[10 ... 99] = 2,[100] = 3};
块引用>注意:该数组的长度为一个指定的最高值加。
此外,我们可以结合使用的命名连续元素的普通C初始化元素的这种技术。不具有一个标志的每个初始化元件应用于阵列或结构的下一个连续的元素。例如:
int类型的[6] = {[1] = V1,V2,[4] = V4};
等同于
int类型的[6] = {0,V1,V2,0,V4,0};
标注数组初始化的元素时索引为字符或属于枚举类型特别有用。例如:
INT空白[256] = {[''] = 1,['\\ t'] = 1,['\\ H'] = 1,
['\\ F'] = 1,['\\ n'] = 1,['\\ r'] = 1
};What is the meaning of following Code? Code is from the regression test suite of GCC.
static char * name[] = { [0x80000000] = "bar" };
解决方案In C99 you can specify the array indices to assigned value, For example:
static char * name[] = { [3] = "bar" };
is same as:
static char * name[] = { NULL, NULL, NULL, "bar"};
The size of array is four. Check an example code working at ideaone. In your code array size is
0x80000001
(its an hexadecimal number).
Note: Uninitialized elements initialized with0
.5.20 Designated Initializers:
In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C89 mode as well. This extension is not implemented in GNU C++. To specify an array index, write
[index] =
before the element value. For example,int a[6] = { [4] = 29, [2] = 15 };
is equivalent to
int a[6] = { 0, 0, 15, 0, 29, 0 };
One more interesting declaration is possible in a GNU extension:
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=
.To initialize a range of elements to the same value, write
[first ... last] = value
. For example,int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
Note: that the length of the array is the highest value specified plus one.
Additionally, we can combine this technique of naming elements with ordinary C initialization of successive elements. Each initializer element that does not have a designator applies to the next consecutive element of the array or structure. For example:
int a[6] = { [1] = v1, v2, [4] = v4 };
is equivalent to
int a[6] = { 0, v1, v2, 0, v4, 0 };
Labeling the elements of an array initializer is especially useful when the indices are characters or belong to an enum type. For example:
int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\h'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
这篇关于怪阵初始化前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!