avoiding incorrect code
Jed Brown
jed at 59A2.org
Wed Jan 14 17:28:37 CST 2009
On Wed, Jan 14, 2009 at 13:56, Barry Smith <bsmith at mcs.anl.gov> wrote:
> All of this demonstrates that it would be legal that that (!ptr) is not
> correct and one should always use if (ptr == NULL).
FWIW, C99, 6.5.3.3.5 reads
| The result of the logical negation operator ! is 0 if the value of
its operand compares
| unequal to 0, 1 if the value of its operand compares equal to 0. The
result has type int.
| The expression !E is equivalent to (0==E).
In (0 == ptr) the 0 is a null pointer constant (as per 6.5.9.2 `one
operand is a pointer and the other is a null pointer constant') so
there is no reason to prefer (ptr == NULL). Note that in C++, NULL is
a macro which expands to 0, not (void*)0 as is common in C, so that it
doesn't need an explicit cast.
Jed
More information about the petsc-dev
mailing list