On Feb 2, 2004, at 12:00 PM, Helge Hess wrote:
On Feb 2, 2004, at 6:01 AM, Leigh Smith wrote:
Hm, well, actually I do prefer "if (ptr == NULL)" over "if (!ptr)".
Not sure why.
Implicit is the assumption that NULL is always 0, which isn't
actually a specification, merely a convention of the compiler that
NULL is actually something like:
#define NULL (void *) 0
While unusual for a compiler to declare NULL to be something other
than 0, it would be legal C, depending on the processor architecture.
I'd definitely favour the more explicit ptr == NULL since you are
then articulating that you are checking against a NULL pointer,
rather than doing an implicit cast to an integer before then
inverting the implicit comparison against 0.
Good point! Just checked the C99 draft:
http://www.vmunix.com/~gabor/c/draft.html#7.1.6
which indeed proves you right :-) NULL is not required to be defined
as 0.
Yes, but it does not matter. Using "if (ptr)" or "if (!ptr)" will still
work. The first one is equivalent to "if ((ptr) != 0)" but the compiler
will recognize that 0 in this context is a null pointer and use the
correct NULL value.
I still prefer "if (ptr != NULL)", for readability reasons.