在C语言中,是初始化数组只有一个元素经过特殊处理的? [英] In C, is array initialization with only one element treated specially?
问题描述
在阅读<一href=\"http://stackoverflow.com/questions/8060931/append-to-the-beginning-of-an-array/8061034#8061034\">this问题我想测试在GCC的输入,看到错误会是什么输出。令我惊讶的下面一行:
字符数组[] = {的};
编译没有错误或警告,导致含有S \\ 0
2大小的数组。我本来期望一个编译器错误,因为前pression右侧的类型为的char * []
。
是一个数组初始化与不被视为在这种情况下一个数组,为什么?
只有一个元素 字符数组[] = {的};
是一样的:
字符数组[] =S;
下面 {}
在此情况下可选的,因为s为字符串文字。
或者
字符数组[] = {'S','\\ 0'};
在这种情况下, {}
是必要的初始化数组
While reading this question I wanted to test the input in GCC to see what errors would be output. To my surprise the following line:
char array[] = {"s"};
compiles without error or warning, resulting in an array of size 2 containing "s\0"
. I would have expected a compiler error because the right side of the expression is of type char*[]
.
Is an array initialization with only one element not treated as an array in this case, and why?
char array[] = {"s"};
is same as:
char array[] = "s";
Here { }
are optional in this case because "s" is string literal.
Or,
char array[] = {'s', '\0'};
In this case, { }
are necessary to initialize the array.
这篇关于在C语言中,是初始化数组只有一个元素经过特殊处理的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!