offsets in structures.

Ron Natalie <ron> ron at brl-tgr.ARPA
Fri Oct 19 11:06:00 AEST 1984


> >A problem with expressions like
> >
> >        &((foo *)0)->bar
> > ...
> 
> Perhaps more robust would be  ((char *)&((foo *)0)->bar - (char *)(foo *)0)
> 
> so that, regardless of the value of ((foo *)0), all that is expected of the
> compiler is that &((foo *)0)->bar be produced by adding the offset to bar
> to the value of ((foo *)0).  There is still the presumption that any pointer
> can be converted to (char *) without loss of information.

But nowhere does the spec say that saying 0->anything or *(pointer_cast)0
is guaranteed to valid at all.  Zero can be put into a pointer so that it
can be checked for later, period.  On certain architectures (like ones that
have different pointer layouts for various types) trying to 0-> something
may not be valid.  Consider the HEP:  The pointers work as follows:

      Char    SHORT   Medium  int (and long)
	0	1	2	0
	1
	2	3
	3
	4	5	6
	5
	6	7
	7
	8			8

In these cases something that begins with short or medium would have
a zero pointer of 1 or 2 even though the compiler traps the special
NULL (0) case and handles it.



More information about the Comp.lang.c mailing list