简体   繁体   English

“NUL”字符(“\0”)字节是分隔的还是分配给带有字符串的字符数组?

[英]Is the “NUL” character (“\0”) byte separated or is it assigned to that char-array with the string?

If i define a string:如果我定义一个字符串:

   char array[5] = {"hello"};

Is the NUL character (\0) byte "hidden" added to "array[5]", so that the array is not contained of 5 bytes in memory, it is contained of 6 bytes?是否将 NUL 字符 (\0) 字节“隐藏”添加到“array[5]”中,使得 memory 中的数组不包含 5 个字节,而是包含 6 个字节?

OR does the NUL character byte lie "separated" from "array[5]" in memory only after the last element of the char-array, but not directly assigned to "array[5]"?或者 NUL 字符字节是否与 memory 中的“array[5]”“分开”仅在 char 数组的最后一个元素之后,但不直接分配给“array[5]”?

If i would put this:如果我会这样说:

  i = strlen(array);
  printf("The Amount of bytes preserved for array: %d",i);

What would be the result for the amount of bytes preserved for array[5]?为数组 [5] 保留的字节数会是什么结果?

Does the "NUL" character ("\0") byte lie separated after the last element of char-array in the memory or is it assigned to that char-array? “NUL”字符(“\0”)字节是否位于 memory 中 char 数组的最后一个元素之后分隔,还是分配给该 char 数组?

Does the "NUL" character ("\0") byte lie separated after the last element of char-array in the memory or is it assigned to that char-array? “NUL”字符(“\0”)字节是否位于 memory 中 char 数组的最后一个元素之后分隔,还是分配给该 char 数组?

No. Neither answer is correct.不,两个答案都不正确。 See below for details.详情见下文。


Answer for C: C 的答案:

If you write your code like that, with an explicit size that is too small for the terminator, array will have exactly 5 elements and there will be no NUL character.如果您这样编写代码,并且显式大小对于终止符来说太小,则array将恰好有 5 个元素,并且不会有 NUL 字符。

strlen(array) has undefined behavior because array is not a string (it has no terminator). strlen(array)具有未定义的行为,因为array不是字符串(它没有终止符)。 char array[5] = {"hello"}; is equivalent to char array[5] = {'h', 'e', 'l', 'l', 'o'};相当于char array[5] = {'h', 'e', 'l', 'l', 'o'}; . .

On the other hand, if you write另一方面,如果你写

char array[] = "hello";

it is equivalent to它相当于

char array[6] = {'h', 'e', 'l', 'l', 'o', '\0'};

and strlen will report 5 . strlen将报告5

The relevant part of the C standard is: C标准的相关部分是:

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.字符串文字的连续字符(包括终止 null 字符,如果有空间或如果数组大小未知)初始化数组的元素。

(Emphasis mine.) (强调我的。)


Answer for C++: C++ 的答案:

Your code is invalid.您的代码无效。 [dcl.init.string] states: [dcl.init.string]状态:

There shall not be more initializers than there are array elements.初始化器的个数不得多于数组元素的个数。 [ Example: [示例:

 char cv[4] = "asdf"; // error

is ill-formed since there is no space for the implied trailing '\0' .格式不正确,因为隐含的尾随'\0'没有空间。 end example ] 结束示例]

In C++, char array[5] = {"hello"} is of six bytes.在 C++ 中, char array[5] = {"hello"}为 6 个字节。 But you have assigned five bytes only.但是您只分配了五个字节。 Therefore, the array declaration is incorrect.因此,数组声明不正确。

Alternatively, this works: char array[6] = {"hello"} .或者,这可行: char array[6] = {"hello"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM