这个问题困扰了我一段时间。我从未见过 NULL 的不同定义,它总是
#define NULL ((void *) 0)
是否有任何体系结构对 NULL 进行了不同的定义,如果是这样,为什么编译器不为我们声明这一点?
C 2011 标准,在线草稿
6.3.2.3 Pointers
...
3 An integer constant expression with the value 0, or such an expression cast to type
void *
, is called a null pointer constant.66) If a null pointer constant is converted to a
pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
66) The macro NULL
is defined in <stddef.h>
(and other headers) as a null pointer constant; see 7.19.
The macro NULL
is always定义为零值常量表达式;它可以是裸 0,或 0 转换为void *
,或其他一些计算结果为 0 的整数表达式。就您而言源代码被关注到,NULL
将始终评估为 0。
一旦代码被翻译,任何出现的空指针常量(0,NULL
等)将被替换为底层架构用于空指针的任何内容,该指针可能是也可能不是 0 值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)