What C compilers have non-zero null pointers?

Gary Ansok ansok at stsci.EDU
Mon Jul 23 01:18:53 AEST 1990


In article <12288 at netcom.UUCP> ergo at netcom.UUCP (Isaac Rabinovitch) writes:
>True.  But what the "NULL should always be 0" diehards want is not to
>write (for example)
>
>	for (ptr = fist; ptr != 0; ptr = ptr->next)
>
>in which 0 should probably be #DEFINED anyway, but rather
>
>	for (ptr = first; ptr ; ptr = ptr->next)
>
>which produces tighter code and (most important of all) looks
>spiffier.  It has the elegance of expression old C hands crave.

Once more with feeling:

	if (ptr)		/* or for(;ptr;) */

is exactly equivalent to

	if (ptr != 0)

which is exactly equivalent to

	if (ptr != (typeof ptr) 0)

which is exactly equivalent to

	if (ptr != NULL-pointer-for-typeof-ptr)

Any C compiler that has a not-all-bits-zero NULL internal representation
and does not compare a pointer to that in "if (ptr)" or "for (...; ptr; ...)"
is seriously BROKEN.

Whether you like "if (ptr)" on readability grounds is a different question
(I like it, but I seem to be in the minority) -- but that's purely a style 
question and the compiler had better produce the correct code.

Gary Ansok
ansok at stsci.edu



More information about the Comp.lang.c mailing list