What is the really pointing to?

Rhys Weatherley rhys at batserver.cs.uq.oz.au
Wed May 9 13:51:33 AEST 1990


pete at 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!!



More information about the Comp.lang.c mailing list