[comp.lang.c] cast and subtract in offsetof macro

throopw@sheol.UUCP (Wayne Throop) (04/15/91)

> steve@taumet.com (Stephen Clamage)
>> ((int) (((char *) (&(((struct small_struct*) 0)->y))) - ((char *) 0)))
> The cast to char* and subtraction of zero don't do anything useful.

"That turns out not to be the case."

The cast to (char*) makes this implementation more portable to those
machines that have word-oriented addresses.  The subtraction of
a zero (cast, again, to (char*)) makes it more portable to those
machines where the null pointer may not have the bit-pattern of
an integer zero.

It may be argued that this extra portability isn't "useful", but
I think it was one of the goals of the people who created this macro.
--
Wayne Throop  ...!mcnc!dg-rtp!sheol!throopw