Must a NULL pointer be a 0 bit pattern?

Henry Spencer henry at utzoo.UUCP
Sun Nov 4 10:05:28 AEST 1984

> 1. Because of implicit comparison with zero, as in "while(p)s;", this idea
> is cannot be implemented simply by changing stdio.h to read
> #define NULL ((char*)0x87654321)

Quite correct.  The compiler itself has to know what the bit pattern of
the 0 pointer is, so that it can generate correct code for implicit
comparisons against 0.

> 2. In making changes to the compiler, this must remain zero:
> 	(p = NULL , (int)p)
> Also, for every declared "var", this must remain one:
> 	(p = &var , (int)p != 0)

Sorry, wrong.  The results of casting a pointer to integer are explicitly
implementation-defined, except for the ability -- in certain limited
circumstances -- to cast it back to pointer and get the same one you
started with.  There are *no* *guarantees* anywhere about the results
of the comparisons you give, and compilers for odd machines are entitled
to give results other than the ones you suggest.

The key point in all of this is that the constant 0 does not necessarily
have the same representation in all types.
				Henry Spencer @ U of Toronto Zoology

More information about the Comp.lang.c mailing list