C++:使用sizeof的char数组的大小

Saa*_*aad 8 c++ arrays string sizeof

在C++中查看以下代码:

char a1[] = {'a','b','c'};
char a2[] = "abc";
cout << sizeof(a1) << endl << sizeof(a2) << endl;
Run Code Online (Sandbox Code Playgroud)

虽然sizeof(char)是1个字节,但为什么输出显示sizeof(a2)为4而不是3(如果是a1)?

Pub*_*bby 17

C字符串包含空终止符,因此添加了一个字符.

基本上这个:

char a2[] = {'a','b','c','\0'};
Run Code Online (Sandbox Code Playgroud)