In C, what is the difference between a NULL pointer and a pointer that points to 0?
相关问题
- Multiple sockets for clients to connect to
- Do the Java Integer and Double objects have unnece
- What means in Dart static type and why it differs
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
The old comp.lang.c FAQ has a big section on the null pointer and it's worth a read.
comp.lang.c null pointers
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.
The ISO/IEC 9899:TC2 states in
6.3.2.3 Pointers
The macro NULL expands to an implementation-defined null pointer constant.
Any two null pointers shall compare equal.
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 whichNULL
was other than 0, but that is not mandated by the standard.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.Evaluating the literal
0
in a pointer context is identical toNULL
. Whatever bit pattern the compiler uses to represent aNULL
pointer is hidden.