finding offset of a member in C structures

Pyung-Chul Kim pckim at unisql.UUCP
Sat May 18 06:13:38 AEST 1991


I have a question about finding offset of a member in a structure of C.
For example, in the following structure, `a_struct',

	struct a_struct {
		short	member1;
		char	member2;
		int	memner3;
	};

can I get the offset of member2 at *compiling time* and *portably* ?
The offset may be different for machines and compilers because
they employ different member alignment schemes.
I think there is no reason for C compiler not to provide such facility.
It is possible to provide a compiler operator:
	offsetof(member_name,structure_type_name)
as it provides 'sizeof' operator.
Do you know if there is a compiler operator like above, or is there any
alternative solution.

Thanks in advance.

P.S.
we can get the offset at run_time like following:
#define offsetof(a,b)	((int)&((b *)0->a))
But, what I mean is *at compile time*.

-- 
Pyung-Chul Kim

UniSQL, Inc.
9390 Research Blvd., Kaleido II, Suite 220, Austin, TX 78759
Internet: execu!sequoia!unisql!pckim at cs.utexas.edu
UUCP: {uunet, cs.utexas.edu!execu}!sequoia!unisql!pckim
TEL: (512)343-7297 Ext. 332
FAX: (512)343-7383



More information about the Comp.lang.c mailing list