NULL、'' 和 0 有什么区别? [英] What is the difference between NULL, '' and 0?

查看:47
本文介绍了NULL、'' 和 0 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中,各种零值之间似乎存在差异——NULLNUL0.

In C, there appear to be differences between various values of zero -- NULL, NUL and 0.

我知道 ASCII 字符 '0' 的计算结果为 480x30.

I know that the ASCII character '0' evaluates to 48 or 0x30.

NULL指针通常定义为:

#define NULL 0

或者

#define NULL (void *)0

此外,还有一个 NUL 字符 '' 似乎也等于 0.

In addition, there is the NUL character '' which seems to evaluate to 0 as well.

这三个值有不相等的时候吗?

Are there times when these three values can not be equal?

在 64 位系统上也是如此吗?

Is this also true on 64 bit systems?

推荐答案

注意:此答案适用于 C 语言,不适用于 C++.

Note: This answer applies to the C language, not C++.

整数常量文字 0 有不同的含义,具体取决于使用它的上下文.在所有情况下,它仍然是一个值为 0 的整数常量,只是描述方式不同.

The integer constant literal 0 has different meanings depending upon the context in which it's used. In all cases, it is still an integer constant with the value 0, it is just described in different ways.

如果一个指针与常量字面量0进行比较,那么这是一个检查指针是否为空指针.这个 0 然后被称为空指针常量.C 标准定义 0 转换为 void * 类型既是空指针又是空指针常量.

If a pointer is being compared to the constant literal 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The C standard defines that 0 cast to the type void * is both a null pointer and a null pointer constant.

此外,为了提高可读性,在头文件 stddef.h 中提供了宏 NULL.根据您的编译器,可能会 #undef NULL 并将其重新定义为古怪的东西.

Additionally, to help readability, the macro NULL is provided in the header file stddef.h. Depending upon your compiler it might be possible to #undef NULL and redefine it to something wacky.

因此,这里有一些检查空指针的有效方法:

Therefore, here are some valid ways to check for a null pointer:

if (pointer == NULL)

NULL 被定义为比较等于一个空指针.NULL 的实际定义是由实现定义的,只要它是一个有效的空指针常量.

NULL is defined to compare equal to a null pointer. It is implementation defined what the actual definition of NULL is, as long as it is a valid null pointer constant.

if (pointer == 0)

0 是空指针常量的另一种表示形式.

0 is another representation of the null pointer constant.

if (!pointer)

这个 if 语句隐式检查不是 0",因此我们将其反转为为 0".

This if statement implicitly checks "is not 0", so we reverse that to mean "is 0".

以下是检查空指针的无效方法:

The following are INVALID ways to check for a null pointer:

int mynull = 0;
<some code>
if (pointer == mynull)

对编译器来说,这不是检查空指针,而是检查两个变量的相等性.如果 mynull 从未在代码中更改并且编译器优化常量将 0 折叠到 if 语句中,则此 可能 起作用,但这不能保证并且编译器必须产生至少一条诊断消息(警告或错误) 根据 C 标准.

To the compiler this is not a check for a null pointer, but an equality check on two variables. This might work if mynull never changes in the code and the compiler optimizations constant fold the 0 into the if statement, but this is not guaranteed and the compiler has to produce at least one diagnostic message (warning or error) according to the C Standard.

请注意,C 语言中空指针的值与底层架构无关.如果底层架构有一个空指针值定义为地址 0xDEADBEEF,那么就由编译器来解决这个问题.

Note that the value of a null pointer in the C language does not matter on the underlying architecture. If the underlying architecture has a null pointer value defined as address 0xDEADBEEF, then it is up to the compiler to sort this mess out.

因此,即使在这个有趣的架构上,以下方法仍然是检查空指针的有效方法:

As such, even on this funny architecture, the following ways are still valid ways to check for a null pointer:

if (!pointer)
if (pointer == NULL)
if (pointer == 0)

以下是检查空指针的无效方法:

The following are INVALID ways to check for a null pointer:

#define MYNULL (void *) 0xDEADBEEF
if (pointer == MYNULL)
if (pointer == 0xDEADBEEF)

因为这些被编译器视为正常的比较.

as these are seen by a compiler as normal comparisons.

'' 被定义为空字符 - 即所有位都设置为零的字符.'' (与所有字符文字一样)是一个整数常量,在本例中其值为 0.所以 '' 完全等同于一个朴素的 0 整数常量 - 唯一的区别在于它传达给人类读者的 intent(我将其用作空字符.").

'' is defined to be a null character - that is a character with all bits set to zero. '' is (like all character literals) an integer constant, in this case with the value zero. So '' is completely equivalent to an unadorned 0 integer constant - the only difference is in the intent that it conveys to a human reader ("I'm using this as a null character.").

'' 与指针无关.但是,您可能会看到类似于以下代码的内容:

'' has nothing to do with pointers. However, you may see something similar to this code:

if (!*char_pointer)

检查 char 指针是否指向空字符.

checks if the char pointer is pointing at a null character.

if (*char_pointer)

检查 char 指针是否指向非空字符.

checks if the char pointer is pointing at a non-null character.

不要将这些与空指针混淆.仅仅因为位表示是相同的,并且这允许一些方便的交叉情况,它们实际上并不是一回事.

Don't get these confused with null pointers. Just because the bit representation is the same, and this allows for some convenient cross over cases, they are not really the same thing.

请参阅 comp.lang.c 常见问题解答中的问题 5.3 了解更多信息.有关 C 标准,请参阅 此 pdf.查看第 6.3.2.3 节指针,第 3 节.

See Question 5.3 of the comp.lang.c FAQ for more. See this pdf for the C standard. Check out sections 6.3.2.3 Pointers, paragraph 3.

这篇关于NULL、'' 和 0 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆