Xref: utzoo comp.lang.c:13897 comp.lang.c++:1905 Path: utzoo!attcan!uunet!husc6!bloom-beacon!mit-eddie!rutgers!njin!princeton!njsmu!klg From: klg@njsmu.UUCP (Kenneth Goodwin) Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: Something new for C? Summary: offsetof() Keywords: offset of vars within structures Message-ID: <481@njsmu.UUCP> Date: 8 Nov 88 15:33:26 GMT References: <73@dsoft.UUCP> Organization: NJ State Medical Underwriters, Lawrenceville Lines: 33 In article <73@dsoft.UUCP>, root@dsoft.UUCP (Super user) writes: >was a precompiler command to allow me to use > the offset of an item into a structure. for example: > > struct test { > long this; > int that; > char those[8]; > }; > > val = offset(test,that); Until they actually stick something like this into the compiler , try this: #define offsetof(Struct, Member) (&((struct Struct *)0)->Member) I believe it;s parenthesized correctly. declares 0 to be a pointer of type struct Struct and computes the address of Member within the structure. since 0 + anything = anything what you get (obviously) is the offset of the member within the structure. Since all kludges have an inherent risk of non-portability comments on the above method are welcome. usage: value = offsetof(test, that); Ken Goodwin NJSMU.