if(p)

Lowell Savage savage at ssc-vax.UUCP
Wed Oct 9 03:25:14 AEST 1985


In article <1671 at brl-tgr.ARPA> ukma!david at ANL-MCS.ARPA (David Herron) writes:
> In article <516 at talcott.UUCP> tmb at talcott.UUCP (Thomas M. Breuel) quotes:
> >In article <1671 at brl-tgr.ARPA>, ART at ACC.ARPA (Art Berggreen) writes:
> >> From an abstract language viewpoint, an "if" statement conditionally
> >> executes a block of statements based on whether the control statement
> >> evaluates to a condition of *TRUE*.  Pointers by themself do not
> >> have attributes of TRUE vs FALSE.  Thus, "if(pointer)" makes less semantic
> 
> I disagree...  Pointers have VALID and INVALID values.  So when you
> say if(p) you're asking if it has a VALID or INVALID value.  It's
> just that conveniently most machines have 0 as the INVALID value.

No, C *GUARANTEES* that 0 is an invalid pointer value.  Page 192 of
my copy of K&R (this is the end of section 7.14 of appendix A) says:

	"...  However, it is guaranteed that assignment of the
	constant 0 to a pointer will produce a null pointer dis-
	tinguishable from a pointer to any object."

And page 97 (the middle of section 5.4 Address Arithmetic) says:

	"C guarantees that no pointer that validly points at data
	will contain zero, so a return value of zero can be used to
	signal an abnormal event, ...."

So I agree with David, except that this is not a machine-dependant thing,
it is defined into the C language!!  If the machine doesn't support it
directly, the compiler must generate whatever code it needs to support
"zero pointer = invalid pointer".  So in other words, the compiler has
builtin support for "VALID-pointer = TRUE" and "INVALID-pointer = FALSE",
and I find this concept very useful and helpful--as well as quicker.

				There's more than one way to be savage,

				Lowell Savage



More information about the Comp.lang.c mailing list