无意中,我发现第char s[] = {"Hello World"};
行是正确编译的,似乎与第char s[] = "Hello World";
行的处理方式相同.第一个({"Hello World"}
)不是一个数组,它包含一个char数组元素,所以s的声明应该是char *s[]
?事实上,如果我把它改成char *s[] = {"Hello World"};
,编译器也会像预期的那样接受它.
为了寻找答案,我找到的唯一提到这一点的地方是this one,但没有引用标准.
所以我的问题是,为什么要编译第char s[] = {"Hello World"};
行,而左侧是array of char
类型,右侧是array of array of char
类型?
以下是工作计划:
#include<stdio.h>
int main() {
char s[] = {"Hello World"};
printf("%s", s); // Same output if line above is char s[] = "Hello World";
return 0;
}
谢谢你的澄清.
另外,我的编译器是gcc-4.3.4.