I was wondering if NULL
is guaranteed to be 0
in C++, so I searched and came across these:
This answer states that:
Here is Bjarne Stroustrup's wordings,
In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer.
Which seems to confirm that NULL
is always 0.
But according to cppreference.com:
#define NULL /*implementation-defined*/
The macro NULL is an implementation-defined null pointer constant, which may be:
-> an integral constant expression rvalue of integer type that evaluates to zero (until C++11)
-> an integer literal with value zero, or a prvalue of type std::nullptr_t (since C++11)
And that clearly says NULL
is implementation dependent.
This answer says:
0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things:
f(int); f(foo *);
Which again implies that NULL
is the integer 0
and that might cause ambiguity
There are other questions and answers I encountered, but they are mostly about the C language, not C++. This comment says:
And NULL is guaranteed to be 0
But again, that's about C.
To sum it all up, is NULL
always 0
in C++? What about C? Is it (for every standard implementation) the same as:
#define NULL 0
Note: this question is not about the null pointer, the question is if NULL
in C++ is guaranteed to be 0
the integer. Is it implementation dependent?
The null pointer constant is always 0. The NULL macro may be defined by the implementation as a naked 0 , or a cast expression like (void *) 0 , or some other zero-valued integer expression (hence the "implementation defined" language in the standard). The null pointer value may be something other than 0.
The answer to that is rather simple: a NULL means that there is no value, we're looking at a blank/empty cell, and 0 means the value itself is 0. Considering there is a difference between NULL and 0, the way Tableau treats these two values therefore is different as well.
In C, NULL is a macro that expands either to 0 or (void*)0 (or something that has a similar effect). In the first case, you can not differentiate between NULL and 0 , because they are literally the same.
Comparing any variable with NULL will always evaluate to FALSE, regardless if it's value, unless IS NULL or IS NOT NULL is used. Violating this rule will affect the functionality of the code. The severity is critical which means that the code will not function correctly.
Is NULL guaranteed to be 0?
According to the standard, NULL
is a null pointer constant (i.e. literal). Exactly which one, is implementation defined.
Prior to C++11, null pointer constants were integral constants whose integral value is equal to 0, so 0
or 0l
etc.
Since C++11, there is a new null pointer literal nullptr
and NULL
may be defined as being nullptr
. (And thus literal interpretation of Bjarne's quote has become obsolete).
Prior to standardisation: NULL
may be defined as (void*)0
in C. Since C++ was based on C, it is likely that some C++ dialects pre-dating the standard might have used that definition, but such definition is not conformant with standard C++.
And for completeness: As explained in more detail in SO post linked in a comment below, null pointer constant being 0 does not necessarily mean that the value of the null pointer address is 0 (although the address being 0 is quite typical).
What can be concluded about this:
NULL
to represent the number zero (use 0
with appropriate type suffix if appropriate), nor to represent a null-terminator character (use '\0'
).NULL
resolves to a pointer overload.NULL
but instead use nullptr
if your standard is >= C++11. In older standard you can use (T*)NULL
or (T*)0
if you need it for overload resolution... that said there are probably very few cases where overloading integers with pointers makes any sense.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