if(p)

Roger Hale roger at ll-sst
Tue Oct 8 07:45:21 AEST 1985


There's something I've been missing in the if(p) discussion:
I see where K&R declare that comparing pointers with 0 is special (sec. 7.7),
where ``condition ? pointer : 0'' is special (sec. 7.13),
and where assigning 0 to pointers is special (sec. 7.14);
but I don't see where if(p) invokes the same mechanism.

    Section 7.7 Equality operators:
		expression == expression
		expression != expression
	A pointer may be compared to ... the constant 0 [machine-
    independently].  A pointer to which 0 has been assigned ... will appear
    to be equal to 0[.]

    Section 9.3 Conditional statement:
	if ( expression ) statement
	if ( expression ) statement else statement
    In both cases the expression is evaluated and if it is non-zero,
    the first substatement is executed.  In the second case the second
    substatement is executed if the expression is 0.

    Section 9.4 While statement:
	... remains non-zero ...

    Section 9.5 Do statement:
	... becomes zero ...

    Section 9.6 For statement:
	... becomes 0 ...

In 9.3 ``the expression is 0'' cannot mean the constant 0, but rather
that the expression evaluates to zero.  There are plenty of uses of
``0'', ``zero'', and ``non-zero'' referring to what an expression
evaluates to, and only three references that I've found (7.7, 7.13 and 7.14)
to the special meaning of the constant 0.  Initialization, section 8.6,
refers to assignment's conversions and so incorporates them; but for
the various conditional constructs I don't see how ``evaluates to zero''
can be taken equivalent to ``compares equal to the constant 0''.

In Sum:  it seems to me that ``if (p != NULL)'' is portable (NULL being 0)
			 but ``if (p)'' is not.

Please correct me if I be wrong, before I mislead countless others.

		Roger Hale	<roger at ll-sst.arpa>



More information about the Comp.lang.c mailing list