Is "if (!pointer)" as portable as "if (pointer == NULL)" ???

Karl Heuer karl at haddock.ima.isc.com
Thu Apr 12 02:40:29 AEST 1990


In article <656 at hades.OZ> greyham at hades.OZ (Greyham Stoney) writes:
>But can you also just do an aritmetic check on the cast value of the pointer?:

No.

>K&R says that casting anything with a value 0 to a pointer yeilds a NULL
>pointer; so presumably casting a NULL pointer back to value yeilds a zero.

Not necessarily.

>	if (buffer = malloc(50))	/* yes, that SHOULD be =, not == */
>	if (buffer) free(buffer)

But these are acceptable (if you like that style, which I don't).  You seem to
believe that `if (buffer)' means `if ((int)buffer != 0)' (which would not be
a valid test), but in fact it means `if (buffer != (char *)0)' (which is quite
correct).

Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list