Is it if (pointerVar) is the same as if (pointerVar! = NULL)?

Simple question:

Is if (pointerVar) the same as if (pointerVar!=NULL) ?

Also, if (!pointerVar) same as if (pointerVar==NULL) ?

Give me your technically correct / pedantic answer. These two statements seem and make sense to act the same. Is there something wrong with the previous one, though (apart from its slightly lower readability)?

+6
c ++ pointers
source share
4 answers

For most pedantic answers, the relevant sections of the specification are given here.

First, here is how the if , from & sect; 6.4.4:

The condition value, which is the initialized declaration in a statement other than the switch statement, is the value of the declared variable implicitly converted to type bool . If this transformation is poorly formed, the program is poorly formed.

"But how do pointers to bool s translate?" you can ask. So here & sect; 4.12.1 :: -)

The value of arithmetic, enumeration, pointer or a pointer to a member type can be converted to an rvalue of type bool. A null value, a null pointer value, or a null element pointer value is converted to false; any other value is converted to true.

So that means statement

 if (ptr) 

equivalently

 if ((bool) ptr) 

which in turn is equivalent

 if (ptr == NULL) 

But what about

 if (!ptr) 

Well, the C ++ specification, section 5.3.1.8, says that

The operand of the logical negation operator! implicitly converted to bool (clause 4); its value is true if the converted operand is false and false otherwise. The result type is bool .

So that means that

 if (!ptr) 

equivalently

 if (!(bool)ptr) 

which in turn is equivalent

 if (!(ptr == NULL)) 

which is finally equivalent

 if (ptr != NULL) 

Phew! It was an interesting search. Hope this answers your question!

Of course, this story has more. NULL not part of C ++; this is a macro in <cstddef> defined as

 #define NULL 0 

This works because the C ++ standard defines a null pointer in & sect; 4.10.1 how

The null pointer constant is the integral constant expression (5.19) of the rvalue type of an integer type that evaluates to zero . The null pointer constant can be converted to a pointer type; the result is a null pointer value of this type and is different from any other value of a pointer to an object or a pointer to a function type

So, to be more correct, I had to use the numeric literal 0 in the above examples. However, if you included <cstddef> , then after preprocessing this will work with the same code.

+10
source share

Assuming pointerVar is a raw pointer, then yes. Otherwise, it depends, although usually it is almost the same for smart pointers.

In the first version, of course, there is nothing wrong, because many experienced C ++ programmers consider this to be more readable.

+6
source share

In C ++, NULL expands to 0 or 0L . Thus, if (pointerVar) matches if (pointerVar!=NULL) and if (!pointerVar) matches if (pointerVar==NULLL) .

+1
source share

Yes. You're right. Remember that a condition is false if it returns a null value, otherwise it is true. In the above expressions, when pointerVal = NULL, (NULL is zero inside), if(pointerVal) returns false. The converse is true for if(!pointerVal) , since we deny the condition.
Both expressions are valid and generally accepted.

+1
source share

All Articles