offsets in structures.

John Bruner jdb at mordor.UUCP
Fri Oct 12 05:03:09 AEST 1984


A problem with expressions like

	&((foo *)0)->bar 

(which I confess I have used myself) is that (foo *)0 is NOT a pointer
whose value is zero, it is a special case -- the NULL pointer.  The
two are identical only if your machine represents NULL as an integer
zero.  If your machine has a special representation for NULL/nil
pointers (e.g.  if it is a tagged architecture with a special NULL/nil
pointer tag) then (foo *)0 and (int)0 are distinct.  The above
expression is based upon the use of (foo *)0 as a pointer to zero,
not as the NULL pointer.

I believe that the above expression should be an illegal attempt to
perform address calculation using NULL.  Consider the following
"equivalent" code sequence on a machine for which NULL is not
represented as an integer zero; the address calculation here will blow
up, since "x" is assigned the NULL pointer, not a pointer to zero:

	foo *x;

	x = (foo *)0;
	... = &x->bar;
-- 
  John Bruner (S-1 Project, Lawrence Livermore National Laboratory)
  MILNET: jdb at mordor.ARPA [jdb at s1-c]	(415) 422-0758
  UUCP: ...!ucbvax!dual!mordor!jdb 	...!decvax!decwrl!mordor!jdb



More information about the Comp.lang.c mailing list