hal*_*lex 50 c c++ arrays string array-initialization
偶然地,我发现该行char s[] = {"Hello World"};已经正确编译,似乎被视为相同char s[] = "Hello World";.第一个({"Hello World"})不是包含一个char数组的元素的数组,所以s的声明应该读取char *s[]吗?事实上,如果我将其更改为char *s[] = {"Hello World"};编译器,也会按预期接受它.
寻找答案,我找到的唯一提到这个的地方是这个,但没有引用标准.
所以我的问题是,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;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何澄清.
PS我的编译器是gcc-4.3.4.
Chr*_*oph 64
这是允许的,因为标准是这样说的:C99第6.7.8节,§14:
字符类型数组可以由字符串文字初始化,可选地用大括号括起来.字符串文字的连续字符(如果有空间或数组大小未知,则包括终止空字符)初始化数组的元素.
这意味着两者兼而有之
char s[] = { "Hello World" };
Run Code Online (Sandbox Code Playgroud)
和
char s[] = "Hello World";
Run Code Online (Sandbox Code Playgroud)
只不过是语法糖
char s[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', 0 };
Run Code Online (Sandbox Code Playgroud)
在相关的注释(同一节,§11),C也允许标量初始化器周围的大括号
int foo = { 42 };
Run Code Online (Sandbox Code Playgroud)
顺便说一下,它与复合文字的语法非常吻合
(int){ 42 }
Run Code Online (Sandbox Code Playgroud)
Naw*_*waz 22
大括号是可选的,表达式只相当于一个char数组.
你也可以这样写:
int a = {100}; //ok
Run Code Online (Sandbox Code Playgroud)
实际上,C++11概括了这种语法,统一地初始化非数组和数组.那么C++11,你可以拥有这些:
int a{}; //a is initialized to zero, and it is NOT an array
int b[]{1,2,3,4}; //b is an array of size 4 containing elements 1,2,3,4
int c[10]{}; //all 10 elements are initialized to zero
int *d{}; //pointer initialized to nullptr
std::vector<int> v{1,2,3,4,5}; //vector is initialized uniformly as well.
Run Code Online (Sandbox Code Playgroud)