cast and subtract in offsetof macro (was: Taking address of array)

Wayne Throop throopw at sheol.UUCP
Mon Apr 15 13:24:34 AEST 1991


> steve at 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



More information about the Comp.lang.c mailing list