我应该写“0x0”还是“0”,而不是 NULL?
我知道你应该在 C++ 中使用 0
而不是 NULL (尽管 NULL
在 C++ 中大多数时候被定义为 0
) 。
不过,最近我遇到了一些使用 0x0
的代码。
有什么区别?
I know that you are supposed to use 0
instead of NULL in c++ (even though NULL
is defined as 0
in C++ most of the time).
Recently I came across some code where 0x0
was used instead, though.
What is the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
顺便说一句,大家,
0x0 是十六进制,正如你们都提到的,但
0 是八进制,而不是十进制! :-)
即任何以 0 开头(且后面不跟 x)的数字都是八进制:
:-)
By the way, everyone,
0x0 is hex, as you have all mentioned, but
0 is Octal, not decimal! :-)
ie any number starting with 0 (and not followed by x) is octal:
:-)
0x0
和0
代表相同的值,因此您可以使用最适合您眼睛的值。当我们获得 C++0x 时,我们将使用关键字nullptr
来表示任何类型的空指针。0x0
and0
represent the same value, so you can use the one that best suits your eye. When we get C++0x, we'll have the keywordnullptr
to represent null pointers of any type.0x0 只是十六进制值 0 的表达。我怀疑编译器会关心这种差异。
0x0 is just an expression of the value 0 in hexadecimal. I doubt the compiler will care about the difference.
没有区别
在我看来,0x0 更明确。
有些人可能会将 0 与“0”混淆(0x30 -我见过这种情况发生)。
No difference
In my opinion, 0x0 is more explicit.
Some people may confuse 0 with "0"(0x30 -I´ve seen it happen).
简而言之,这是一个品味问题。人们喜欢用十六进制书写内存地址,因此将 NULL 写为 0x0 会更经典。对于我这样的普通人来说,0就够了。
Simply put, its a matter of taste. People like to write memory addresses in hexa decimal, so writing NULL as 0x0 would be more classic. For ordinary ones like me, 0 will suffice.
所以,这个网站有一些关于 NULL 的非常好的信息: http://c-faq.com/ null/index.html
NULL 实际上是“空指针” - 因此它可能是“(void*)0”类型。这告诉编译器不要将 NULL 视为 int,而是将其视为“指向 int 的指针”。在 C 中,如果类型不匹配,编译器将给出警告/错误。
值“0”和“0x0”在 C++ 中是等效的。每当一个数字以“0x”为前缀时,就意味着该值是该数字的十六进制表示形式。例如,0x5 == 5。
此外,0xA [以十六进制表示,基数为 16) == 10 [以基数 10 表示]。
编辑
以下是一些来源。在glibc中,NULL似乎在不同的地方有不同的定义。 (为什么会这样?)
#define NULL ((void *)0) (stdlib/gmp-impl.h)
#define NULL 0 (posix/getopt1.c)
edit2
好吧,看起来就像我错过了这个标记一样!对不起! (CW'd。请随意继续投票!)
So, this site has some really good information on NULL: http://c-faq.com/null/index.html
NULL is actually the "null pointer" - so it might be of type, for example, "(void*)0". This tells the compiler that NULL is not to be treated as an int, but rather as a "pointer to an int". And in C, the compiler will give warnings/errors if the types don't match.
The value "0" and "0x0" are equivalent in C++. Whenever a number is prefixed by "0x", it means that that value is the hexadecimal representation of that number. For example, 0x5 == 5.
Also, 0xA [in hexadecimal, base 16) == 10 [in base 10].
edit
Here are some sources. In glibc, NULL seems to be defined differently in different places. (why should this be?)
#define NULL ((void *)0) (stdlib/gmp-impl.h)
#define NULL 0 (posix/getopt1.c)
edit2
well, it looks like i've missed the mark on this one! sorry! (CW'd. feel free to keep downvoting!)
0x0
就是以十六进制表示的0
。两者没有区别:NULL
通常被#define
改为0
某处并做同样的事情。0x0
is just0
written in hexadecimal notation. There is no difference between the two:NULL
is usually#define
d to0
somewhere and does the same thing.没有什么区别。在 C 中,NULL 通常被定义为
(void *)0
,但在 C++ 中这是不允许的。一些古老的编译器犯了这个错误,但它们确实很古老。IMO,最好使用 NULL,因为它更清楚地描述意图,并且当您的编译器更新到 C++ 0x 时,它会为您提供一个漂亮、简单的 S&R 符号,其中将包括
nullptr。
There is no difference. In C, NULL is often defined to be
(void *)0
, but in C++ that's not allowed. A few ancient compilers got this wrong, but they really are ancient.IMO, it's better to use NULL, as it portrays the intent more clearly, and gives you a nice, easy symbol to S&R when your compiler gets updated to C++ 0x, which will include
nullptr
.