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

diamond@tkovoa diamond at tkou02.enet.dec.com
Wed Apr 11 15:06:18 AEST 1990


In article <656 at hades.OZ> greyham at hades.OZ (Greyham Stoney) writes:

>	if ((buffer = malloc(50)) != NULL)
>But can you also just do an aritmetic check on the cast value of the pointer?:
>	if (buffer = malloc(50))	/* yes, that SHOULD be =, not == */

Yes it is portable, just not readable.  They both have the same effect,
and should generate identical code.  But you notice that the second one
is longer, because you have to include that comment in order to make it
almost as readable as the first one.

>	if (buffer) free(buffer)

This is also portable, and almost readable.  Again, to be really readable,
you should still compare to NULL, but I grant that it would make your
source code longer this time.

-- 
Norman Diamond, Nihon DEC     diamond at tkou02.enet.dec.com
This_blank_intentionally_left_underlined________________________________________



More information about the Comp.lang.c mailing list