Taking address of array

Wolfram Roesler wolfram at cip-s08.informatik.rwth-aachen.de
Fri Apr 12 21:58:43 AEST 1991


graham at 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



More information about the Comp.lang.c mailing list