Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!sdd.hp.com!caen!ox.com!math.fu-berlin.de!unidui!unido!rwthinf!cip-s08!wolfram From: wolfram@cip-s08.informatik.rwth-aachen.de (Wolfram Roesler) Newsgroups: comp.lang.c Subject: Re: Taking address of array Message-ID: Date: 12 Apr 91 11:58:43 GMT References: <1991Apr9.172534.28982@tcom.stc.co.uk> Sender: news@rwthinf.UUCP Lines: 38 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? An & before an array fails on all compilers I know. In your example, if s is of type small_struct, `s.y' is a pointer to the first element of the array, and `&s.y' is illegal. Try `& s.y[0]' instead. I'm afraid you will need to write a new macro, one that will determine the offset of an array within a struct. Suppose you used the macro #define offset(s,e) (int) (&(((s*)0)->e) - 0) (parantheses and casts omitted for clarity) do determine the offset of element e in struct s, you will have to write a new macro #define aoffset(s,e) (int) ((((s*)0)->e) - 0) ^ '&' omitted if e is an array. So: copy the original definition of your macro, give it a new name and erase the '&' and use this one for arrays. This should work. Grettings Okami-san f