什么是'\\ 0'和0 NULL之间的区别, [英] What is the difference between NULL, '\0' and 0

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

问题描述

在C,似乎有零各种值之间的差异 - NULL NUL 0

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

我知道,ASCII字符 0 计算结果为 48 的0x30

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

NULL 指针通常被定义为:

#define NULL 0

或者

#define NULL (void *)0

此外,还有就是 NUL 字符'\\ 0'这似乎计算为 0 以及

是不是有时候这三个值不能等于?

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 强制转换为类型无效* 既是一个空指针和一个空指针常量。

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.

此外,以帮助可读性,宏 NULL 在头文件中提供的 STDDEF.H 。这取决于你的编译器有可能为和#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. Anyone doing this deserves to be shot.

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

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 是空指针常量的另一个重presentation。

0 is another representation of the null pointer constant.

if (!pointer)

如果语句隐式检查不为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永远不会改变的code和编译器优化恒定的折叠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'被定义为一个空字符 - 这是设置为零的所有位字符。这有没有关系的指针。然而,你可能会看到类似这样的code的内容:

'\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

不要混淆这些空指针。仅仅因为有点重presentation是相同的,这允许在某些情况下方便的交,他们是不是真的同样的事情。

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 整型常量 - 唯一不同的是在意图的,它传达给人类读者(我用这个作为空字符)。

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.

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

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