how has C bitten you? (Really,

lee at eel.UUCP lee at eel.UUCP
Mon Sep 2 11:11:00 AEST 1985


	One final, subtle, point.  K&R does not guarantee that the *value* 0
	is distinguishable from all other pointers, but rather, that the
	*constant* 0 is.  That is to say, you may compare against 0 to
	determine the validity of a pointer (or assign to guarantee
	invalidity), but you may not assume that comparison against (or
	assignment of) an int variable whose value is 0 will have the same
	result.  This picky distinction probably doesn't affect any of the
	better known chips, but might be important on a machine where a null
	pointer is not a bit string of 0s.

While the quotation is true, I think that it refers to the automatic
coercion that is required to give the constant 0 the proper distinguishable
pattern in the appropriate pointer type.  I think we all fairly assume that

	char *p=0, *q="a";
	main() {if (p==q) printf("bogus");}

will fail to print because one of the pointers has been assigned the
constant 0 and one has been assigned a pointer to a real object.  Therefore
the value 0 does persist after assignment to any pointer type and is
distinguishable from the values in other pointers as well.  And two such
pointers to the same type both of which have been assigned the value 0
will compare equal.

I don't see why the restriction applies to non-pointer variables.  As long as
type coercions are explicit, this should apply to all values of zero, whether
encountered as a literal in the program or as the value of a variable of
integral type.

I think it is not unreasonable, tho it is certainly not covered anywhere,
that coercions between pointers of different types should map the 0 value
properly so that, for example,

	int *p=0;
	char *q=0;
	main() {if (p==(int *)q) printf("this is right");}

should produce output.  We all know that 0 cannot be interpreted as a
pointer without knowing what it is a pointer to, but given that we know the
types of the pointers involved, the "I don't point to anything" values
should be considered equivalent in assignments and comparisons.



More information about the Comp.lang.c mailing list