简体   繁体   English

初始化具有显式大小的char数组并初始化为大于该大小

[英]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.

相关问题 fget读取的字符数大于存储大小 - fgets where the number of char read is bigger than the size of the storage 可以在没有明确大小的情况下初始化C中的二维数组吗? - Can a two-dimensional array in C be initialized without explicit size? 如果像这样初始化,则重新分配2d数组:char(* A)[size] = malloc(sizeof(char [size] [size])) - Reallocating a 2d array if initialized like so: char (*A)[size] = malloc(sizeof(char[size][size])) 初始化分配数组的大小 - initializing allocating the size of an array 为什么连接和复制大于数组大小的字符串没有错误? - Why no error in concatenating & copying strings bigger than array size? 可以使用不同大小的字符串文字初始化具有给定大小的静态char数组会导致未定义的行为吗? - Could initializing a static char array with a given size using a string literal of a different size cause undefined behavior? 当填充后面跟着一个大于整数的字符时,填充不应用于结构体中的整数 - Padding not applied on integer in a struct when it is followed by a char of size bigger than integer 为什么当元素大小大于实际值大小时,`g_array_append_val`会出现段错误? - Why does `g_array_append_val` segfault when the element size is bigger than the actual value size? realloc()问题:旧块的释放,旧大小的旧大小,以及传递静态数组基址 - realloc() issues: deallocation of old block,new size bigger than old size,and passing static array base address C 已初始化和未初始化的可变大小数组 - C initialized and non initialized array with variable size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM