Need macro to get offset within a structure

Richard Murnane richardm at runx.oz.au
Sun Feb 17 10:27:13 AEST 1991


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 at runxtsa.runx.oz       - Just Say No.
Packet Radio: VK2JRM @ VK2OP.NSW.AUS.OC



More information about the Comp.lang.c mailing list