how has C bitten you? (Really,

darryl%ism780.uucp at BRL.ARPA darryl%ism780.uucp at BRL.ARPA
Mon Sep 9 06:55:52 AEST 1985


>>                                  K&R does not guarantee that the *value* 0
>>       is distinguishable from all other pointers, but rather, that the
>>       *constant* 0 is.
>>
>> 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.
>
>As for "why" - think of a machine where a null pointer *didn't* have the
>same bit pattern as the integer 0.  Every time you assigned an integer to a
>pointer, you'd have to check whether the integer was zero or not and assign
>a null pointer instead (unless the computation you had to do to convert an
>integer to a pointer did this anyway).

Exactly, but also consider what K&R says in section 7.14:

    The compilers currently allow a pointer to be assigned to an integer, an
    integer to a pointer, and a pointer to a pointer of another type.  The
    assignment is a pure copy operation, with no conversion.

Also, in section 14.4:

    A pointer may be converted to any of the integral types large enough to
    hold it. [...]  The mapping function is also machine dependent, but is
    intended to be unsurprising to those who know the addressing structure
    of the machine.

Although this does not seal it up completely, it seems that K&R had it in
mind that putting pointers into integers (and taking them back again) would
have no overhead.  Checking for a 0 *value* probably is more overhead than
they had in mind.

	    --Darryl Richman, INTERACTIVE Systems Corp.
	    ...!cca!ima!ism780!darryl
	    The views expressed above are my opinions only.



More information about the Comp.lang.c mailing list