Use Pointer Value as Condition to Check if Pointer Is NULL in C++ Null pointers are evaluated as false when they are used in logical expressions. Thus, we can put a given pointer in the if statement condition to check if it's null.
Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null. Hence Pointer to a null pointer is not only valid but important concept.
Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type ( int * , char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.
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.
You can; the null pointer is implicitly converted into boolean false while non-null pointers are converted into true. From the C++11 standard, section on Boolean Conversions:
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type
bool
. A zero value, null pointer value, or null member pointer value is converted tofalse
; any other value is converted totrue
. A prvalue of typestd::nullptr_t
can be converted to a prvalue of typebool
; the resulting value isfalse
.
Yes, you could.
This is part of the C++ standard conversion, which falls in Boolean conversion clause:
§ 4.12 Boolean conversions
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.
Yes, you can. In fact, I prefer to use if(pointer)
because it's simpler to read and write once you get used to it.
Also note that C++11 introduced nullptr
which is preferred over NULL
.
Question is answered, but I would like to add my points.
I will always prefer if(pointer)
instead of if(pointer != NULL)
and if(!pointer)
instead of if(pointer == NULL)
:
Less chances to write a buggy code, suppose if I misspelled equality check operator ==
with =
if(pointer == NULL)
can be misspelled if(pointer = NULL)
So I will avoid it, best is just if(pointer)
.
(I also suggested some Yoda condition in one answer, but that is diffrent matter)
Similarly for while (node != NULL && node->data == key)
, I will simply write while (node && node->data == key)
that is more obvious to me (shows that using short-circuit).
Explicitly checking for NULL could provide a hint to the compiler on what you are trying to do, ergo leading to being less error-prone.
Yes, you can. The ability to compare values to zeros implicitly has been inherited from C, and is there in all versions of C++. You can also use if (!pointer)
to check pointers for NULL.
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