[comp.std.c] null pointers of various types.

pardo@june.cs.washington.edu (David Keppel) (03/27/89)

This is a request for information.

I recently made the following assertions about pANSI C.
Are they correct?

* The number zero (0) compares equal with any null pointer of any type.
* The bit storage of a null pointer is not necessarily all zeros.
* Null pointers of two types do not necessarily compare equal.
* A null pointer of one type, cast in to another pointer type, will
  always be cast to a null pointer in the destination type.

	;-D on  ( All this, for a bunch of zeros! )  Pardo
-- 
		    pardo@cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo

bph@buengc.BU.EDU (Blair P. Houghton) (03/28/89)

In article <7672@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes:
>I recently made the following assertions about pANSI C.
>Are they correct?
>
>* The number zero (0) compares equal with any null pointer of any type.
>* Null pointers of two types do not necessarily compare equal.

If this is true, how long will it be until someone accidentally uses
this fact to prove that one can violate the first and second laws of
thermodynamics?

				--Blair
				  "...since the zeroth is apparently
				   conditional on data typing..."

henry@utzoo.uucp (Henry Spencer) (03/28/89)

In article <7672@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes:
>I recently made the following assertions about pANSI C.
>Are they correct?
>
>* The number zero (0) compares equal with any null pointer of any type.

The integer *constant* 0, an integer *constant* expression that evaluates
to 0, or either of those cast to "void *", compares equal with any 
null pointer.  Comparing a general number, e.g. an int variable, to a
pointer is illegal.  And there is no guarantee that casting a general
zero value to a pointer will produce a null pointer.

The real problem here is that C has the notion of a null pointer constant,
which is a magic value of pointer type, but instead of writing it as "nil"
like some other languages do, C says that an integer constant with the
value zero, when found in a pointer context, means null.  The transformation
into the magic pointer value is done at compile time, not at run time, so
just any zero won't do -- it has to be something that the compiler knows
to be zero.  It's just an odd way of writing the null pointer *constant*;
it does not imply any general relationship between integer zeros and null
pointers.

>* The bit storage of a null pointer is not necessarily all zeros.

Correct.

>* Null pointers of two types do not necessarily compare equal.

Comparing two pointers of different types is, in general, illegal.
The results of casting a pointer of one type to a different type is,
in general, implementation-defined, but there is a specific exemption
for null pointers.  Any two null pointers, when cast to pointer types
that are sufficiently similar for comparison to be legal, compare equal.

>* A null pointer of one type, cast in to another pointer type, will
>  always be cast to a null pointer in the destination type.

I do not actually find this to be guaranteed in the general case,
although the requirement that it compare equal to a null pointer of
the destination type will convince most implementors to make it so.
The two properties of null pointers are that they compare equal to
each other, and that they compare unequal to a pointer to any object
or function (subject, in all cases, to the requirement that types be
similar enough for comparison to be legal).  The wording of 3.2.2.3
guarantees that casting to other pointer types preserves the first
property, but the draft is silent about the effects of general pointer
casts on the second property.

(Note that a null pointer constant does not become a null pointer until
it is converted to a specific pointer type, so *that* transformation
is exempt from this.)

(Note also that some specific pointer casts carry stronger guarantees.)

(The above is based on the Oct draft, but is probably correct for more
recent versions too.)
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu