[英]Initializing a char array with an explicit size and initialized to bigger than the size
I've been reading some code and I encountered the following: 我一直在阅读一些代码,我遇到了以下内容:
int function(){
char str[4] = "ABC\0";
int number;
/* .... */
}
Normally, when you write a string literal to initialize a char array, the string should be null terminated implicitly right? 通常,当你编写一个字符串文字来初始化一个char数组时,该字符串应该是null隐式终止对吗? What happens in this case?
在这种情况下会发生什么? Does the compiler recognize the '\\0' in the string literal and make that the null terminator?
编译器是否识别字符串文字中的'\\ 0'并使其成为空终止符? or does it overflow to the int number?
还是溢出到int数? Is there anything wrong with this form?
这个表格有什么问题吗?
If the code is: 如果代码是:
char str[3] = "ABC";
It's fine in C, but the character array str
is not a string because it's not null-terminated. 它在C中很好,但字符数组
str
不是字符串,因为它不是以空值终止的。 See C FAQ: Is char a[3] = "abc"; 见C FAQ:char a [3] =“abc”; legal?
法律? What does it mean?
这是什么意思? for detail.
细节。
In your example: 在你的例子中:
char str[4] = "ABC\0";
The last character of the array str
happens to be set to '\\0'
, so it's fine and it's a string. 数组
str
的最后一个字符碰巧被设置为'\\0'
,所以它很好,它是一个字符串。
The C99 standard §6.7.8.¶14 says C99标准§6.7.8.¶14说
An array of character type may be initialized by a character string literal, optionally enclosed in braces.
字符类型数组可以由字符串文字初始化,可选地用大括号括起来。 Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
字符串文字的连续字符(如果有空间或数组大小未知,则包括终止空字符)初始化数组的元素。
This means that the following statements are equivalent. 这意味着以下陈述是等效的。
char str[4] = "ABC\0";
// equivalent to
char str[4] = "ABC";
// equivalent to
char sr[4] = {'A', 'B', 'C', '\0'};
So there's nothing wrong with the first statement above. 所以上面的第一个陈述没有错。 As the standard explicitly states, only that many characters in the string literal are used for initializing the array as the size of the array.
正如标准明确指出的那样,只有字符串文字中的那么多字符用于初始化数组作为数组的大小。 Note that the string literal
"ABC\\0"
actually contains five characters. 请注意,字符串文字
"ABC\\0"
实际上包含五个字符。 '\\0'
is just like any character, so it's fine. '\\0'
就像任何角色一样,所以没关系。
However please note that there's a difference between 但请注意,两者之间存在差异
char str[4] = "ABC\0";
// equivalent to
char str[4] = {'A', 'B', 'C', '\0'};
char str[] = "ABC\0"; // sizeof(str) is 5
// equivalent to
char str[] = {'A', 'B', 'C', '\0', '\0'};
That's because the string literal "ABC\\0"
contains 5
characters and all these characters are used in the initialization of str
when the size of the array str
is not specified. 这是因为字符串文字
"ABC\\0"
包含5
字符,当未指定数组str
的大小时,所有这些字符都用于str
的初始化。 Contrary to this, when the size of str
is explicitly stated as 4
, then only the first 4
characters in the literal "ABC\\0"
are used for its initialization as clearly mentioned in the above quoted para from the standard. 与此相反,当
str
的大小明确地表示为4
,则只有文字"ABC\\0"
中的前4
字符用于其初始化,如上面引用的标准段中明确提到的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.