pointers, tests, casts

Chris Torek chris at mimsy.UUCP
Wed Nov 23 08:53:02 AEST 1988


In article <11130 at dartvax.Dartmouth.EDU> ericb at libdev (Eric Bivona) writes:
>I know about casting NULL or 0 to get an appropriate nil pointer ...

Note that a cast is always sufficient, if not always necessary.  (I
find that much `extra' casting hampers readability, though.)

[edited]
>	if (ptr == (char *)0) {		/* or (char *)NULL */

>In assignments, a 0 or NULL is cast implicitly to the correct pointer
>type ....

Right.

>What about the '==' comparison above?  Would "(ptr == 0)" get evaluated
>correctly?

It would; the implicit cast occurs here, too.

>... what about ... "(!ptr)"?

That too is correct, by the rule that `!x' means `x == 0'.  Stylistically,
though, I prefer `if (ptr == NULL)'.

In fact, the only place that `0' does not implicitly turn into the right
kind of `nil' is in function calls (and, under dpANS rules, only those
where prototypes are not present or insufficient to convey the necessary
type information).
-- 
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