Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!spool.mu.edu!uunet!munnari.oz.au!metro!ipso!runxtsa!richardm From: richardm@runx.oz.au (Richard Murnane) Newsgroups: comp.lang.c Subject: Need macro to get offset within a structure Message-ID: <1991Feb16.232713.6001@runx.oz.au> Date: 16 Feb 91 23:27:13 GMT Sender: richardm@runx.oz.au (Richard Murnane) Organization: RUNX Un*x Timeshare. Sydney, Australia. Lines: 37 Hi all, Does anybody know a decent macro to find the offset of an element in a structure, e.g. if I have a structure typedef struct ABC { int a; char b[5]; float c; } ABC_STRUCT; I need a macro hich will let me say OFFSET(ABC_STRUCT,c), which will give me the offset of the float in the above structure. I have tried something like this: #define OFFSET(s,e) & (((s *) NULL)->e) and the above followed by -NULL to force the compiler to evaluate the macro as the difference of two addresses (and to avoid problems with different memory models). Microsoft C 5.1 chokes on the macro, when I try to initialise elements of a table with such an expression (although if I remove the "&", as I might to evaluate the address of the array b in the structure, the compiler accepts it. However, I'm looking for something that will work for the general case). I vaguely remember doing something like this for a brain-dead Intermetrics compiler a few years ago, so it must be possible :-) I just don't remember how! Thanks in advance, - Richard -- Richard P. Murnane "Let's just be friends." ? Internet: richardm@runxtsa.runx.oz - Just Say No. Packet Radio: VK2JRM @ VK2OP.NSW.AUS.OC