Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!execu!sequoia!unisql!pckim From: pckim@unisql.UUCP (Pyung-Chul Kim) Newsgroups: comp.lang.c Subject: finding offset of a member in C structures Message-ID: <1276@unisql.UUCP> Date: 17 May 91 20:13:38 GMT Reply-To: pckim@unisql.UUCP (Pyung-Chul Kim) Organization: UniSQL, Inc., Austin, Texas Lines: 36 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@cs.utexas.edu UUCP: {uunet, cs.utexas.edu!execu}!sequoia!unisql!pckim TEL: (512)343-7297 Ext. 332 FAX: (512)343-7383