No, you cannot get a valid (=nonnull) pointer p
for which p != NULL
returns false
.
Note, however, that this does not imply anything about "address 0" (whatever that means). C++ is intentionally very abstract when referring to addresses and pointers. There is a null pointer value, i.e. the value of a pointer which does not point anywhere. The way to create such a pointer is to assign a null pointer constant into a pointer variable. In C++03, a null pointer constant was the literal 0
, optionally with a suffix (e.g. 0L
). C++11 added another null pointer constant, the literal nullptr
.
How the null pointer value is represented internally is beyond the scope of the C++ language. On some (most?) systems, the address value 0x00000000
is used for this, since nothing a user program can point to can legally reside at that address. However, there is nothing stopping a hardware platform from using the value 0xDEADBEEF
to represent a null pointer. On such platform, this code:
int *p = 0;
would have to be compiled as assigning the value 0xDEADBEEF
into the 4 bytes occupied by the variable p
.
On such a platform, you could legally get a pointer to address 0x00000000
, but it wouldn't compare equal to 0
, NULL
or nullptr
.
A different way of looking at this is that the following declarations are not equivalent:
int *p = 0;
int *q = reinterpret_cast<int*>(0);
p
is initialised with the null pointer value. q
is initialised with the address 0
(depending on the mapping used in reinterpret_cast
, of course).