Why NULL is 0
Dave Sill
dsill at NSWC-OAS.arpa
Thu Mar 17 02:53:45 AEST 1988
In article <800 at zippy.eecs.umich.edu> Jan Wolter <janc at palam.eecs.umich.EDU> writes:
>One other vaguely related question: Which of the following produce a null
>pointer?
>
> int zero = 0;
> char *p1 = 0; /* this is a null pointer! */
Yes.
> char *p2 = zero; /* is this a null pointer? */
Maybe. K&R say assignments between pointers and ints are nonportable,
as are assignments between different types of pointers.
> char *p3 = (char *)zero; /* what's this? */
Exactly the same as p2. K&R define a cast as performing the
conversions required to assign the operand to a variable of the type
of the cast.
>As I read K&R, a null pointer is only produced when a *constant* 0 is assigned
>to a pointer.
Almost, once generated a null pointer can be produced by assignment.
I.e.,
char *pc1, *pc2;
pc1 = 0;
pc2 = pc1;
>While K&R says assignment is a bitwise copy, they say explicitly typecasting
>an integer to a pointer gives a machine dependent result. Thus it seems
>possible that the p1, p2, and p3 could be three different pointers.
No, at most two different pointers. p1 is null. p2 and p3 will be
the same nonportable pointer.
>(Frankly,
>the more I read on this subject, the more I think K&R didn't have their minds
>entirely clear on this business either.)
Maybe you should read some more.
=========
The opinions expressed above are mine.
"Give me a pointer and I'll dereference the world."
-- David Keppel
More information about the Comp.lang.c
mailing list