什么是“\0”在 C++ 中?
我正在尝试将一个大型项目从 C++ 翻译为 Delphi,并且正在完成翻译。我留下的东西之一是“\0”怪物。
if (*asmcmd=='\0' || *asmcmd==';')
其中 asmcmd 是 char*。
我知道 \0 标志着 C++ 中数组类型的结束,但我需要将其视为一个字节。是0吗?
换句话说,下面的代码是否与 C++ 行等效?
if(asmcmd^=0) or (asmcmd^=';') then ...
其中 asmcmd 是 PAnsiChar。
你不需要知道 Delphi 来回答我的问题,但告诉我 \0 作为字节。那也行。 :)
I'm trying to translate a huge project from C++ to Delphi and I'm finalizing the translation. One of the things I left is the '\0' monster.
if (*asmcmd=='\0' || *asmcmd==';')
where asmcmd is char*.
I know that \0 marks the end of array type in C++, but I need to know it as a byte. Is it 0?
In other words, would the code below be the equivalent of the C++ line?
if(asmcmd^=0) or (asmcmd^=';') then ...
where asmcmd is PAnsiChar.
You need not know Delphi to answer my question, but tell me \0 as byte. That would work also. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
'\0'
等于0
。它是 C 语言的遗物,它根本没有任何字符串类型,而是使用 char 数组。空字符用于标记字符串的结束;回想起来,这不是一个非常明智的决定 - 大多数其他字符串实现在某处使用专用计数器变量,这使得查找字符串末尾的时间复杂度为 O(1),而不是 C 的 O(n)。*asmcmd=='\0'
只是检查length(asmcmd) == 0
或asmcmd.is_empty()
的复杂方法一种假设的语言。'\0'
equals0
. It's a relic from C, which doesn't have any string type at all and uses char arrays instead. The null character is used to mark the end of a string; not a very wise decision in retrospect - most other string implementations use a dedicated counter variable somewhere, which makes finding the end of a string O(1) instead of C's O(n).*asmcmd=='\0'
is just a convoluted way of checkinglength(asmcmd) == 0
orasmcmd.is_empty()
in a hypothetical language.这是
null
的 char 或char
值0
。它用在字符串的末尾。That is the char for
null
orchar
value0
. It is used at the end of the string.严格来说,它是八进制值为零的字符的转义序列(当然在任何基数中也为零)。
尽管您可以使用任何以零为前缀的数字来指定八进制字符代码(例如
'\040'
是 ASCII 编码中的空格字符),但您很少有理由这样做。 '\0' 是指定 NUL 字符的惯用语(因为您无法从键盘键入此类字符或在编辑器中显示它)。您同样可以指定“\x0”,它是以十六进制表示的 NUL 字符。
NUL 字符在 C 和 C++ 中用于终止存储在字符数组中的字符串。此表示形式用于文字字符串常量,并且按照惯例用于由
/
库操作的字符串。在 C++ 中,可以使用 std::string 类来代替。请注意,在 C++ 中,诸如
'\0'
或'a'
之类的字符常量的类型为char
。在 C 中,可能出于一些晦涩的原因,它具有 int 类型。Strictly it is an escape sequence for the character with the octal value zero (which is of course also zero in any base).
Although you can use any number prefixed with zero to specify an octal character code (for example
'\040'
is a space character in ASCII encoding) you would seldom ever have cause to do so. '\0' is idiomatic for specifying a NUL character (because you cannot type such a character from the keyboard or display it in your editor).You could equally specify '\x0', which is a NUL character expressed in hexadecimal.
The NUL character is used in C and C++ to terminate a string stored in a character array. This representation is used for literal string constants and by convention for strings that are manipulated by the
<cstring>
/<string.h>
library. In C++ the std::string class can be used instead.Note that in C++ a character constant such as
'\0'
or'a'
has typechar
. In C, for perhaps obscure reasons, it has typeint
.