[6.3.2.3-3] An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.
这里告诉咱们:0、0L、'\0'、3 - 三、0 * 17 (它们都是“integer constant expression”)以及 (void*)0 (tyc: 我以为(void*)0应该算是一个空指针吧,更恰当一点)等都是空指针常量(注意 (char*) 0 不叫空指针常量,只是一个空指针值)。至于系统选取哪一种形式做为空指针常量使用,则是实现相关的。通常的 C 系统选择 (void*)0 或者 0 的居多(也有个别的选择 0L);至于 C++ 系统,因为存在严格的类型转化的要求,void* 不能象 C 中那样自由转换为其它指针类型,因此一般选 0 做为空指针常量(tyc: C++标准推荐),而不选择 (void*)0。
什么是空指针(null pointer)?
[6.3.2.3-3] 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.
所以,若是 p 是一个指针变量,则 p = 0;、p = 0L;、p = '\0';、p = 3 - 3;、p = 0 * 17; 中的任何一种赋值操做以后(对于 C 来讲还能够是 p = (void*)0;), p 都成为一个空指针,由系统保证空指针不指向任何实际的对象或者函数。反过来讲,任何对象或者函数的地址都不多是空指针。(tyc: 好比这里的(void*)0就是一个空指针。把它理解为null pointer仍是null pointer constant会有微秒的不一样,固然也不是紧要了)
什么是 NULL?
[6.3.2.3-Footnote] The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant
即 NULL 是一个标准规定的宏定义,用来表示空指针常量。所以,除了上面的各类赋值方式以外,还能够用 p = NULL; 来使 p 成为一个空指针。(tyc:不少系统中的实现:#define NULL (void*)0,与这里的“a null pointer constant”并非彻底一致的)
幸运的是,在实际编程中不须要了解在咱们的系统上空指针究竟是一个 zero null pointer 仍是 nonzero null pointer,咱们只须要了解一个指针是不是空指针就能够了——编译器会自动实现其中的转换,为咱们屏蔽其中的实现细节。注意:不要把空指针的内部表示等同于整数 0 的对象表示——如上所述,有时它们是不一样的。
如何判断一个指针是不是一个空指针?
这能够经过与空指针常量或者其它的空指针的比较来实现(注意与空指针的内部表示无关)。例如,假设 p 是一个指针变量,q 是一个同类型的空指针,要检查 p 是不是一个空指针,能够采用下列任意形式之一——它们在实现的功能上都是等价的,所不一样的只是风格的差异。
指针变量 p 是空指针的判断: if ( p == 0 ) if ( p == '\0' ) if ( p == 3 - 3 ) if ( p == NULL ) /* 使用 NULL 必须包含相应的标准库的头文件 */ if ( NULL == p ) if ( !p ) if ( p == q ) ...
指针变量 p 不是空指针的判断: if ( p != 0 ) if ( p != '\0' ) if ( p != 3 - 3 ) if ( p != NULL ) /* 使用 NULL 必须包含相应的标准库的头文件 */ if ( NULL != p ) if ( p ) if ( p != q ) ...
[7.1.3-2] If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.