if (p) ...

Richard_Hargrove harged%ti-eg.csnet at CSNET-RELAY.ARPA
Mon Oct 14 03:12:47 AEST 1985


It seems to me that all the discussion about what
         .
     PTR p;              /* pointer to something... */
         .
         .
     if (p) {
         .
     }

means, whether it is good style, &c... is overlooking something.
Nowhere in K & R is the meaning of the distinguished constant
NULL defined. I thought its definition was implementation
dependent. It just so happens that all implementations I know of
use 0 (or 0L). But nothing *requires* this. That implies to me
that the above coding practice is implementation dependent,
whereas

     if (p != NULL) ...

or

     if (not_null (p)) ...

(given some magic boolean function (or macro) that returns TRUE
if p != NULL else FALSE) are implementation independent. A trivial
macro

#define not_null(p) ((p))

will suffice if your implementation defines NULL as 0.

I won't address the stylistic problems associated with using
non-boolean expressions in a boolean context, regardless of what
the language "allows", but the above macro should solve any
problems in this area also.

regards,
Richard Hargrove    harged%ti-eg
================    ============



More information about the Comp.lang.c mailing list