NULL pointer

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Tue Nov 19 23:31:10 AEST 1985


> But..... for a machine with non-zero null pointer representations
>
> >		lo_func( ..., (int *)NULL, ... );
>
> still doesn't work. What the callee routine gets is a pointer with an all-zero
> representation.... which isn't a null pointer. The ONLY thing that does work is
> 
> 		lo_func( ..., p=0, ....);
> where p is a pointer of the requisite type!  ...

and

> How many times do people have to go over this? Perhaps the problem is that
> people don't understand what a cast does!
> ...

Ron Natalie has already replied restrainedly (for him) to this fool's
posting, so I will simply FLAME HIM for CONFUSING THE POOR NOVICE by
providing MISINFORMATION about what C type casts do.  Dennis Ritchie
about a year ago made one of his rare appearances in this newsgroup
to set this matter straight, and it is covered quite well in all the
standard references for C.

A type cast has the same semantics as assignment to a temporary
variable of the same type as the cast and use of that variable
thereafter in the embedding context.  In particular, this means
that (gleep *)0 is a proper null pointer to a thing of type `gleep';
this is because assignment of the integer constant 0 to a pointer
has those semantics.  There is no assumption whatsoever about "bit
patterns" in any of this!

I try to be polite (for me) in these postings, but this yo-yo has
done a disservice to those readers who do not already know the C
language quite well and who might therefore think that he knows
what he's talking about.

To repeat the CORRECT information that I originally posted, so that
the novice is not left with any misconceptions on the matter:

If a function is declared

	func( ..., foo, ... );
		int *foo;

then invoking it as either

	func( ..., 0, ... );

or

	func( ..., (char *)0, ... );

is simply WRONG, and only works (by accident) on some implementations.
The correct way to pass a null pointer to this function is

	func( ..., (int *)0, ... );

No #definition of NULL (as (char *)0, etc.) can in general keep the
programmer from having to provide this explicit type cast when
passing NULL as an argument to a function.

(In X3J11-conforming implementations, the coercion will be done
automatically if a function prototype declaration is in scope,
but that is a separate matter from the original discussion.)



More information about the Comp.lang.c mailing list