[comp.lang.c] What is the really pointing to?

pete@othello.dartmouth.edu (Pete Schmitt) (05/08/90)

What is the null pointer really pointing to?

If I define the following:

struct entry
{
	int	value;
	struct entry *next;
};

struct entry n1, n2, n3, *list_pointer = &n1;

n1.next = &n2;
n2.next = &n3;
n3.next = (struct entry *) 0;   /* <-- what is this pointing to, address 0? */

john@stat.tamu.edu (John S. Price) (05/08/90)

In article <21910@dartvax.Dartmouth.EDU> pete@othello.dartmouth.edu (Pete Schmitt) writes:
>What is the null pointer really pointing to?
>[declaration deleted]
>struct entry n1, n2, n3, *list_pointer = &n1;
>
>n1.next = &n2;
>n2.next = &n3;
>n3.next = (struct entry *) 0;   /* <-- what is this pointing to, address 0? */

Oh no.. Here we go again.
The (struct entry *) 0 is not address 0.  The NULL pointer is not pointing
to ANYTHING.  Nothing.  Zip.  Zilcho.  It is exactly what it is called.
Nobody really cares what the pointer actually contains, although it
is usually a bit pattern of zero (Yes, I know it's not always.  Segmented
arch. can cause this to be weird)  It doesn't have to be a bit pattern of
zero, though.  But, nobody cares what it is.  THAT pattern, whatever it is,
is the NULL pointer.

C uses 0 to represent for a null pointer for lack of anything better.  0 is 
overloaded to mean two things, depending on it's context.  If it is in an 
integer context, it means the number zero.  If it is used in a pointer context,
as in the above context, then it means a null pointer, whatever that value 
might be.  0 is a SPECIAL CASE to the compiler.

A null pointer doesn't pointer anywhere.  That's its purpose.  To point 
nowhere.  

This was just recently discussed, but I guess that you missed that discussion.


--------------------------------------------------------------------------
John Price                   |   It infuriates me to be wrong
john@stat.tamu.edu           |   when I know I'm right....
--------------------------------------------------------------------------

rhys@batserver.cs.uq.oz.au (Rhys Weatherley) (05/09/90)

pete@othello.dartmouth.edu (Pete Schmitt) writes:

>What is the null pointer really pointing to?

>If I define the following:

>struct entry
>{
>	int	value;
>	struct entry *next;
>};

>struct entry n1, n2, n3, *list_pointer = &n1;

>n1.next = &n2;
>n2.next = &n3;
>n3.next = (struct entry *) 0;   /* <-- what is this pointing to, address 0? */

(struct entry *) 0 will indeed be pointing to address 0 in the virtual
address space of the machine, which MAY NOT necessarily be the
same as the value NULL, but usually is.  Whenever wanting to assign
a NULL pointer value you SHOULD use NULL, even if you know it is
0, because sooner or later someone is going to want to port your
fantastic program to a different machine that doesn't use 0 for
NULL in its C compiler, which could cause some interesting results,
especially if later in the program you were to compare 'n3.next'
with NULL, instead of (struct entry *) 0!  As a similar example, 
in Modula-2 compilers 0 is usually used as NIL (the Modula-2 
equivalent of NULL), but I have seen at least one that uses 
0x80000000 (hex) as its NIL value, so it is always best in whatever 
language you are using, especially C, to use NULL (or some 
equivalent), even if it is less typing to use 0.

Rhys Weatherley, University of Queensland, Australia.  G'day!!