negative addresses (real C null pointers)

Radford Neal radford at calgary.UUCP
Fri May 20 07:45:10 AEST 1988


In article <10001 at tekecs.TEK.COM>, andrew at frip.gwd.tek.com (Andrew Klossner) writes:

> >> Unfortunately, it is a real problem, because there are zillions of
> >> programs that implicitly assume that [null] pointers are all-zeros.
> 
> > I don't think this is true.  How about an example?
> 
> Sure Doug, from the system V kernel... In routine ttout:
> 
> 		if (tbuf->c_ptr)

My understanding is that this is standards-conforming and portable.
I assume that c_ptr is declared to be of pointer type. The above 
statement is equivalent to

		if (tbuf->c_ptr!=0)

which is equivalent to

		if (tbuf->c_ptr!=(char*)0)

(or (int*)0 or whatever). The expression (char*)0 is _defined_ to be
the null pointer, whatever its bit pattern may be. Note that "NULL"
has nothing to do with anything, being merely a macro found in the
<stdio.h> include file.

An example of a non-portable program is the following:

    char *p;
    int i;
    ...
    i = (int)p;
    if (i!=0)
       *p = ...; /* no guarantee that p is not null... */

Only occurences of 0 in the explicit or implicit context (...*) 0
are magically converted to null pointers.

    Radford Neal



More information about the Comp.lang.c mailing list