offsetof() operator. A tricky question.

Sean Fagan seanf at sco.COM
Mon Mar 19 22:22:40 AEST 1990


In article <28008 at cup.portal.com> ts at cup.portal.com (Tim W Smith) writes:
>It IS simple.  It is also wrong.
>
>If you want to have at least a small chance of working across different
>machines, try this:
>
>	offsetof_member0 = (ulong)&ap->member0 - (ulong)ap;

You mean like a '386 Large model program (or a Cyber 180-state, take your
pick)?  Where sizeof(ulong) == 4 and sizeof(void *) == (6 or 8, depending on
machine)?  (Well, ok, the example wouldn't cause problems on the '386, but
it could, I think, on the Cyber...)

If you want to *try* to be portable, see if your machine has a ptrdiff_t
somewhere (I'm assuming non-ANSI, since, otherwise you would automatically
use ptrdiff_t and offsetof()), or, if you have gcc, use the typeof
extension.  Otherwise, hack it 8-).

-- 

-----------------+
Sean Eric Fagan  | "Time has little to do with infinity and jelly donuts."
seanf at sco.COM    |    -- Thomas Magnum (Tom Selleck), _Magnum, P.I._
(408) 458-1422   | Any opinions expressed are my own, not my employers'.



More information about the Comp.lang.c mailing list