Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!zaphod.mps.ohio-state.edu!swrinde!ucsd!rutgers!orstcs!mist.CS.ORST.EDU!osbornk From: osbornk@mist.CS.ORST.EDU (Kasey S. Osborn) Newsgroups: comp.lang.c Subject: Re: finding offset of a member in C structures Message-ID: <1991May20.201857.635@lynx.CS.ORST.EDU> Date: 20 May 91 20:18:57 GMT References: <1276@unisql.UUCP> <16194@smoke.brl.mil> Sender: @lynx.CS.ORST.EDU Organization: Computer Science Department, Oregon State Univ. Lines: 27 Nntp-Posting-Host: mist.cs.orst.edu In article <16194@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: |In article <1276@unisql.UUCP> pckim@unisql.UUCP (Pyung-Chul Kim) writes: |-struct a_struct { |-short member1; |-char member2; |-int member3; |-}; |-can I get the offset of member2 at *compiling time* and *portably* ? | |Yes, but you shouldn't very often need to do so. | |-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. | |Standard C provides an offsetof() macro, but you don't really need one. | struct a_struct foo; | int offset2 = (char *)&foo.member2 - (char *)&foo; /* for example */ Really? I can't find a reference to offsetof() in any of my C references. In fact, GNU CC does not preprocess offsetof() and instead treats it as an external reference. Are you sure this is standard? What compiler are you using? What is the syntax of offsetof()? I've been trying to find a palatable solution to this problem with no luck.