C++ “\0”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14183445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does '\0' mean?
提问by halostack
I can't understand what the '\0' in the two different place mean in the following code:
我无法理解以下代码中两个不同位置的 '\0' 是什么意思:
string x = "hhhdef\n";
cout << x << endl;
x[3]='char *c =”Hello”; // it is actually Hello##代码##
char c[] = {‘Y','o','##代码##′};
';
cout << x << endl;
cout<<"hhh##代码##defef\n"<<endl;
Result:
结果:
hhhdef
hhhef
hhh
高清
哈哈
呵呵
Can anyone give me some pointers?
谁能给我一些指点?
回答by Matteo Italia
C++ std::string
s are "counted" strings - i.e., their length is stored as an integer, and they can contain any character. When you replace the third character with a \0
nothing special happens - it's printed as if it was any other character (in particular, your console simply ignores it).
C++ std::string
s 是“计数”字符串——即,它们的长度存储为整数,并且它们可以包含任何字符。当您将第三个字符替换为\0
没有什么特别之处时 - 它会像打印任何其他字符一样打印(特别是,您的控制台只是忽略它)。
In the last line, instead, you are printing a C string, whose end is determined by the first \0
that is found. In such a case, cout
goes on printing characters until it finds a \0
, which, in your case, is after the third h
.
相反,在最后一行中,您正在打印一个 C 字符串,其结尾由\0
找到的第一个字符串确定。在这种情况下,cout
继续打印字符,直到找到 a \0
,在您的情况下,它在第三个之后h
。
回答by Konrad Rudolph
C++ has two string types:
C++ 有两种字符串类型:
The built-in C-style null-terminated strings which are really just byte arrays and the C++ standard library std::string
class which is notnull terminated.
内置的 C 风格空终止字符串实际上只是字节数组和非空终止的 C++ 标准库std::string
类。
Printing a null-terminated string prints everything up until the first null character. Printing a std::string
prints the whole string, regardless of null characters in its middle.
打印以空字符结尾的字符串会打印所有内容,直到第一个空字符为止。打印 a 会std::string
打印整个字符串,而不管其中间的空字符如何。
回答by cmc
\0
is the NULL character, you can find it in your ASCII table
, it has the value 0.
\0
是 NULL 字符,您可以在您的 中找到它ASCII table
,它的值为 0。
It is used to determinate the end of C-style strings.
它用于确定 C 样式字符串的结尾。
However, C++ class std::string
stores its size as an integer, and thus does not rely on it.
但是,C++ 类std::string
将其大小存储为整数,因此不依赖于它。
回答by Oliver Charlesworth
You're representing strings in two different ways here, which is why the behaviour differs.
您在这里以两种不同的方式表示字符串,这就是行为不同的原因。
The second one is easier to explain; it's a C-style raw char array. In a C-style string, '\0'
denotes the null terminator; it's used to mark the end of the string. So any functions that process/display strings will stop as soon as they hit it (which is why your last string is truncated).
第二个更容易解释;它是一个 C 风格的原始字符数组。在 C 风格的字符串中,'\0'
表示空终止符;它用于标记字符串的结尾。因此,任何处理/显示字符串的函数都会在遇到它时立即停止(这就是为什么最后一个字符串被截断的原因)。
The first example is creating a fully-formed C++ std::string
object. These don't assign any special meaning to '\0'
(they don't have null terminators).
第一个示例是创建一个完整的 C++std::string
对象。它们没有赋予任何特殊含义'\0'
(它们没有空终止符)。
回答by Divya Prakash
The \0
is treated as NULL
Character. It is used to mark the end of the string in C.
将\0
被视为NULL
字符。它用于在 C 中标记字符串的结尾。
In C, string is a pointer pointing to array of characters with \0
at the end. So following will be valid representation of strings in C.
在 C 中,字符串是一个指向字符数组的指针,结尾是字符数组\0
。因此,以下将是 C 中字符串的有效表示。
The applications of ‘\0' lies in determining the end of string .For eg : finding the length of string.
'\0' 的应用在于确定字符串的结尾。例如:求字符串的长度。