In C, what is the difference between a NULL pointer and a pointer that points to 0?
A null pointer constant is an integer constant expression that evaluates to zero. For example, a null pointer constant can be 0, 0L , or such an expression that can be cast to type (void *)0 .
Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C.
The null pointer is basically used in a program to assign the value 0 to a pointer variable of any data type. The void pointer, on the other hand, has no value assigned to it and we use it to store the addresses of other variables in the program- irrespective of their data types.
A pointer can also be initialized to null using any integer constant expression that evaluates to 0, for example char *a=0; . Such a pointer is a null pointer. It does not point to any object.
The ISO/IEC 9899:TC2 states in 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.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
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 which NULL
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With