The Real OS/2 Guy said:
So you should write
if ((p != NULL) == !TRUE)
Is at least more readable as it clearly thays that the condition is
true when the compare to NULL is true. But then you shoud write for
more clarity
if (((p != NULL) == !TRUE) == !TRUE)
But then why stop by the 3 compares? Increase it to an unlimited
amount of compares to get it really clear. Amny != is superflous but
confues to expoermented C programmmer in first step. if (p) is sowhat
of clear because ther is no need to compare the test result with
something. The test itself is anthing needed
Presumably you meant to write
if ((p != NULL) == TRUE)
rather than
if ((p != NULL) == !TRUE)
Presumably you also intended to provide a definition for TRUE, ideally
something simple like
#define TRUE 1
Given that correction, you're absolutely right on the technical issue;
"if (p)" and "if (p != NULL)" are equivalent (assuming that p is a
pointer, and assuming an appropriate #include directive so the NULL
macro is visible).
As far as the style issue is concerned, I'm not going to say that
you're wrong, but my opinion does differ from yours.
The fact that a pointer value can be used as a condition is perfectly
well defined by the C standard. It's something that all C programmers
need to be aware of (as I think everyone who's responded on this
thread already is). Anyone encountering "if (p)" in a C program
shouldn't have any trouble figuring out what it means.
However, just because C allows you to be terse, that doesn't mean you
need to be as terse as possible at all times.
Like Richard, I prefer not to take advantage of C's ability to use a
pointer value as a condition in code that I write. I prefer to make
the comparison to NULL explicit. For one thing, it makes it clearer
to the reader that p is a pointer and not an int or a boolean. My
code is likely to be read more times than it's written; if a couple of
extra tokens make it easier to read, I'll gladly spend the half-second
or so it takes to add them.
Similarly, I prefer to compare characters explicitly to '\0' (if I'm
using them as characters and not as tiny integers), and floating-point
values to 0.0.
My preference in this matter is not very strong, and I don't try to
impose it on anyone else. I can happily read code using either style.
I suspect most C programmers can do so as well.
(On the other hand, comparing something that's already boolean to true
or false is silly and potentially dangerous; that's a preference I
have sometimes tried to impose on others.)
[...]
if (p) means 'is p a valid pointer then ....'
if (!p) means 'is p an invalid pointer then ....'
What can be more clean as an self documenting statement?
if (p != NULL) means 'if p is not NULL' That is an question nobody
asks really.
C has no way to determine whether a pointer is "valid". If p is an
invalid non-null pointer (for example, if it was never initialized or
if it's been passed to free()), then "if (p)" and "if (p != NULL)" are
still precisely equvalent. In principle, testing such a pointer value
invokes undefined behavior; in practice, the condition is likely to
evaluate as true.