Something new for C?
Kenneth Goodwin
klg at njsmu.UUCP
Wed Nov 9 01:33:26 AEST 1988
In article <73 at dsoft.UUCP>, root at 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.
More information about the Comp.lang.c
mailing list