Need macro to get offset within a structure

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Thu Feb 21 16:48:44 AEST 1991


In article <ENAG.91Feb19131201 at holmenkollen.ifi.uio.no>, enag at ifi.uio.no (Erik Naggum) writes:
> I snatched this from /local/lib/gcc-include/stddef.h.
> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
> This works like a charm on a vast number of compilers I've used.

offsetof() being a required part of ANSI C (if you use the right header),
it's clearly the right interface.  You'll want an #if __STDC__ so that you
get the "official" version when it's there, as the cast of zero is not
portable.
-- 
Professional programming is paranoid programming



More information about the Comp.lang.c mailing list