tags:

views:

118

answers:

4

Is this a valid null check on a DOUBLE pointer parameter passed in a function

if (!pdblValue) return E_POINTER;
+1  A: 

Yes. Pointer evaluates to "yes" if it's non-zero, like every other primitive type in C++.

Pavel Radzivilovsky
+6  A: 

Yes, this is normal check for a pointer being non-null.

jpalecek
A: 

Assuming pdblValue is a pointer and it is set to NULL when initialised and invalid then yes.

Konrad
+3  A: 

In C++ the "if" statement uses a boolean expression. If it is true, it executes the part inside the conditional block. If it is false, it doesn't.

pdblValue is of type pointer. Negating it (!pdblValue) will give a long integer expression which is different to zero if the value of the pointer was NULL, and zero otherwise.

This will be converted into a boolean because that is what is needed in the condition. The different from zero value will be converted to true, and the zero value, to false.

Hence, this will have the same semantics than the more natural form:

if (pdblValue == NULL) ...

In which you are actually providing a boolean expression - the one that you were actually meaning.

Daniel Daranas