use of if (!cptr) and if (cptr), where cptr is a *

Chris Torek chris at mimsy.UUCP
Tue Jul 25 15:21:52 AEST 1989


In article <1989Jul24.194646.3012 at nc386.uucp> jeffl at nc386.uucp (Jeff Leyser)
writes:
>OK, this may be meaningless, but out of curiosity is:
>
>	if (cptr == (int) 0)
>
>illegal C, or simply compleat garbage?

[where cptr was declared with `char *cptr;']

If it has a meaning, it means to compare cptr against a nil of type
pointer-to-char.  The question comes down to `is (int)0 an integral
constant expression with value zero', because the way one writes the
untyped nil pointer in C is to write an integral constant expression
whose value is zero.

Clearly (int)0 is an integral expression whose values is zero.  Whether
the cast takes away its `constant-ness' is less certain.  Different
compilers have had differing opinions in the past; what the current
pANS says I am not sure (my copy is one or two revisions out of date,
and is elsewhere at the moment anyway).

The short answer is

	if (cptr == (int)0)

is probably meaningful garbage. :-)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list