if(p)

Chris Torek chris at umcp-cs.UUCP
Fri Oct 18 10:23:53 AEST 1985


> From: ross at smeagol.UUCP (Gary_Ross)

> As I understand it, the C compiler interprets pointers as integers
> in terms of evaluating an if statement.

Your C compiler does, and mine does; but `the' C compiler does not.

> This means that if the value of a pointer is null, the result of:
>			if (p) statement1
>			else statement2
> will execute statement2

Correct.

> because a null pointer has value 0.

Wrong.  Not because a null pointer has the integer value zero, but
because the C language requires this.  On a sufficiently bizarre
machine, the null pointer might be represented by the double-precision
floating-point value 1.179e+7001; yet on such a machine, if the
compiler is correct, and the value of a pointer `p' is nil---compares
equal to zero, so that `(p == 0)' is true---then `if (p) s1; else
s2;' will execute s2.

Of course, most if not all current architectures do indeed represent
a null pointer with 0 or 0L; this makes code generation simpler.
But it is not required.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list