offsets in structures.

Sam Kendall kendall at wjh12.UUCP
Sun Oct 14 05:28:01 AEST 1984


> A better expression, which is a bit closer to legit ...
> ... for getting the offset of a struct element,
>     (char *)(&((struct foo *)0)->element) - (char *)0
> ...
> (2) It is not at all clear that any operation on a NULL pointer other than
>     comparison and asignment is allowed. The above code does a pointer+int,
>     then a pointer difference, both using NULL pointers.
>                      Kevin Martin, UofW Software Development Group

(2) is correct because, it seems to me, if the reference manual defines
no operations on a null pointer besides copying, equality comparison,
and casting, then no other operations make sense (looking only at the
language rather than at implementations of it).

   The expression can be made portable, at least under UNIX, by
having
	extern end;
somewhere in a header file, and then replacing "0" in the expression by
"&end".  For maximum portability, of course, use a real object of type
struct foo in the expression.

	Sam Kendall	  {allegra,ihnp4,ima,amd}!wjh12!kendall
	Delft Consulting Corp.	    decvax!genrad!wjh12!kendall



More information about the Comp.lang.c mailing list