Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!batcomputer!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: finding offset of a member in C structures Message-ID: <5859@goanna.cs.rmit.oz.au> Date: 21 May 91 00:35:23 GMT References: <1276@unisql.UUCP> <16194@smoke.brl.mil> <1991May20.201857.635@lynx.CS.ORST.EDU> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 52 In article <1276@unisql.UUCP> pckim@unisql.UUCP (Pyung-Chul Kim) writes: > can I get the offset of member2 at *compiling time* and *portably* ? In article <16194@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: > Yes, but you shouldn't very often need to do so. > Standard C provides an offsetof() macro, but you don't really need one. In article <1991May20.201857.635@lynx.CS.ORST.EDU>, osbornk@mist.CS.ORST.EDU (Kasey S. Osborn) writes: > 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? It sounds as though you need some up-to-date C references. The little Plauger & Brodie handbook is cheap and excellent. The GNU C compiler *does* understand offsetof, *provided* you #include the right header file. Doug Gwyn wrote "Standard C provides an offsetof() MACRO" and he chose his words carefully. *None* of the standard C macros is available *unless* you #include an appropriate header file. In this case the file in question is , and our copy of GCC certainly has it. > What compiler are you using? What is the syntax of offsetof()? offsetof(TYPE, MEMBER) where TYPE is a type (either a typedef id or 'struct Tag'), not a structure tag. Example: #include struct a_struct { short member1; char member2; int member3; }; typedef struct a_struct a_type; main() { printf("%d %d %d\n", offsetof(struct a_struct, member1), offsetof(struct a_struct, member2), offsetof(a_type, member3)); exit(0); } On an Encore Multimax I get the output "0 2 4". -- There is no such thing as a balanced ecology; ecosystems are chaotic.