In C, what is the difference between a NULL pointer and a pointer that points to 0?
在C中,NULL指针和指向0的指针有什么区别?
5 个解决方案
#1
7
The ISO/IEC 9899:TC2 states in 6.3.2.3 Pointers
ISO / IEC 9899:TC2在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.55) 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
3值为0的整型常量表达式或类型为void *的表达式称为空指针常量.55)如果将空指针常量转换为指针类型,则生成的指针称为空指针,保证比较不等于指向任何对象或函数的指针
The macro NULL expands to an implementation-defined null pointer constant.
宏NULL扩展为实现定义的空指针常量。
Any two null pointers shall compare equal.
任何两个空指针都应该相等。
#2
6
Yes there is. The standard dictates that NULL
always points to invalid memory. But it does not state that the integer representation of the pointer must be 0. I've never come across an implementation for which NULL
was other than 0, but that is not mandated by the standard.
就在这里。标准规定NULL始终指向无效内存。但是它没有说明指针的整数表示必须是0.我从来没有遇到过NULL不是0的实现,但这并不是标准规定的。
Note that assigning the literal 0
to a pointer does not mean that the pointer assumes the integer representation of 0. It means that the special null pointer value is assigned to the pointer variable.
请注意,将文字0指定给指针并不意味着指针假定整数表示为0.这意味着将特殊的空指针值赋给指针变量。
#3
3
Evaluating the literal 0
in a pointer context is identical to NULL
. Whatever bit pattern the compiler uses to represent a NULL
pointer is hidden.
在指针上下文中计算文字0与NULL相同。无论编译器用于表示NULL指针的任何位模式都被隐藏。
#4
1
The old comp.lang.c FAQ has a big section on the null pointer and it's worth a read.
旧的comp.lang.c FAQ在空指针上有一个很大的部分,值得一读。
comp.lang.c空指针
#5
0
The idea is that a NULL
pointer should somehow represent a memory area that is invalid.
So since in the lower memory segments the OS code is mapped, the value of 0 has been used (to represent the NULL pointer) since this area in memory does not belong to the user's program but is mapped to the OS code.
我们的想法是,NULL指针应该以某种方式表示无效的内存区域。因此,在较低的存储器段中映射OS代码,因此使用0的值(表示NULL指针),因为存储器中的该区域不属于用户的程序,而是映射到OS代码。
#1
7
The ISO/IEC 9899:TC2 states in 6.3.2.3 Pointers
ISO / IEC 9899:TC2在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.55) 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
3值为0的整型常量表达式或类型为void *的表达式称为空指针常量.55)如果将空指针常量转换为指针类型,则生成的指针称为空指针,保证比较不等于指向任何对象或函数的指针
The macro NULL expands to an implementation-defined null pointer constant.
宏NULL扩展为实现定义的空指针常量。
Any two null pointers shall compare equal.
任何两个空指针都应该相等。
#2
6
Yes there is. The standard dictates that NULL
always points to invalid memory. But it does not state that the integer representation of the pointer must be 0. I've never come across an implementation for which NULL
was other than 0, but that is not mandated by the standard.
就在这里。标准规定NULL始终指向无效内存。但是它没有说明指针的整数表示必须是0.我从来没有遇到过NULL不是0的实现,但这并不是标准规定的。
Note that assigning the literal 0
to a pointer does not mean that the pointer assumes the integer representation of 0. It means that the special null pointer value is assigned to the pointer variable.
请注意,将文字0指定给指针并不意味着指针假定整数表示为0.这意味着将特殊的空指针值赋给指针变量。
#3
3
Evaluating the literal 0
in a pointer context is identical to NULL
. Whatever bit pattern the compiler uses to represent a NULL
pointer is hidden.
在指针上下文中计算文字0与NULL相同。无论编译器用于表示NULL指针的任何位模式都被隐藏。
#4
1
The old comp.lang.c FAQ has a big section on the null pointer and it's worth a read.
旧的comp.lang.c FAQ在空指针上有一个很大的部分,值得一读。
comp.lang.c空指针
#5
0
The idea is that a NULL
pointer should somehow represent a memory area that is invalid.
So since in the lower memory segments the OS code is mapped, the value of 0 has been used (to represent the NULL pointer) since this area in memory does not belong to the user's program but is mapped to the OS code.
我们的想法是,NULL指针应该以某种方式表示无效的内存区域。因此,在较低的存储器段中映射OS代码,因此使用0的值(表示NULL指针),因为存储器中的该区域不属于用户的程序,而是映射到OS代码。