Path: utzoo!attcan!uunet!sco!seanf From: seanf@sco.COM (Sean Fagan) Newsgroups: comp.lang.c Subject: Re: offsetof() operator. A tricky question. Message-ID: <5246@scolex.sco.COM> Date: 19 Mar 90 12:22:40 GMT References: <8ega02MX90Tk01@amdahl.uts.amdahl.com> <695@shuldig.Huji.Ac.IL> <28008@cup.portal.com> Sender: news@sco.COM Reply-To: seanf@sco.COM (Sean Fagan) Organization: The Santa Cruz Operation, Inc. Lines: 25 In article <28008@cup.portal.com> ts@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@sco.COM | -- Thomas Magnum (Tom Selleck), _Magnum, P.I._ (408) 458-1422 | Any opinions expressed are my own, not my employers'.