Conceptually, NULL
is a singular pointer-value pointing nowhere, and implicitly convertible to any pointer-type.
Practically, you unfortunately cannot depend on it behaving as a pointer value unless you use it in a pointer-only context.
Thus, use nullptr
in C++11 and later respectively 0 cast to the right pointer-type, especially in contexts where an integral zero would not be converted to a pointer.
BTW: While on most modern systems a null pointer is really all-bits-zero, that's not required by the standard (there might be many bit-patterns representing null pointers, and none all-zero), and doesn't make any difference to how a null pointer constant is represented in source code.
In C++:
The macro NULL
is an implementation-defined C++ null pointer constant in this International Standard (4.10).
Which is defined as:
A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t
.
Pre-C++11 the last option was obviously not available...
And post-C++11, because you do not know what it is, you should use nullptr
directly.
In C:
The macros are
NULL
which expands to an implementation-defined null pointer constant; ...
Which is defined as:
An integer constant expression with the value 0, or such an expression cast to type void *
, is called a null pointer constant.