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

查看:145
本文介绍了NULL,'\ 0'和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'似乎也评估为0.

In addition, there is the NUL character '\0' 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标准定义,强制转换为类型void *0既是空指针又是空指针常量.

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 what is a null pointer in the C language. It 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' is defined to be a null character - that is a character with all bits set to zero. This has nothing to do with pointers. However you may see something similar to this code:

if (!*string_pointer)

检查字符串指针是否指向空字符

checks if the string pointer is pointing at a null character

if (*string_pointer)

检查字符串指针是否指向非空字符

checks if the string 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.

此外,'\0'是(像所有字符文字一样)整数常量,在这种情况下,值为零.因此,'\0'完全等同于一个未经修饰的0整数常量-唯一的区别在于它传达给人类读者的 intent (我将其用作空字符". ).

Additionally, '\0' is (like all character literals) an integer constant, in this case with the value zero. So '\0' 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.").

有关更多信息,请参见 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'和0有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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