Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!decwrl!sgi!zok!wattres!steve From: steve@wattres.uucp (Steve Watt) Newsgroups: comp.lang.c Subject: Re: Taking address of array Message-ID: <1991Apr13.050521.7831@wattres.uucp> Date: 13 Apr 91 05:05:21 GMT References: <1991Apr9.172534.28982@tcom.stc.co.uk> Organization: Steven Watt, Consultant, San Jose, CA, USA Lines: 39 In article wolfram@cip-s08.informatik.rwth-aachen.de (Wolfram Roesler) writes: >graham@tcom.stc.co.uk (Graham Bardsley) writes: > >>struct small_struct >>{ >> int x; >> char y[100]; >>}; > >>What I want to know is, if the macro calculates: > >>((int) (((char *) (&(((s*) 0)->y))) - ((char *) 0))) > >>Is the value of this a valid construct which will calculate the offset of y on >>most traditional C compilers? [ slurp! ] >element e in struct s, you will have to write a new macro > #define aoffset(s,e) (int) ((((s*)0)->e) - 0) > ^ > '&' omitted [ and use this slightly different macro on the array elements ] Ick. My much preferred method is to simply say [0] in the invocation of the macro: #define offsetof(t,e) ((size_t)(&(((s) *)0)->e)) /* probably over-parened */ char *p = buf + offsetof(myType, arrayStructMember[0]); ^^^ Seems cleaner than having a *slightly* different macro, and also reminds you that this element is *somewhat* special, but if you change it later, the compiler is more likely to yell at you. -- Steve Watt steve@wattres.UUCP swatt@IBM.COM ...!decwrl!gigo!wattres!steve ...!apple!claris!wattres!steve Never trust a computer bigger than you can lift.