finding offset of a member ...

Dale Worley worley at compass.com
Wed May 22 00:29:48 AEST 1991


In article <1285 at unisql.UUCP> pckim at unisql.UUCP (Pyung-Chul Kim) writes:

   Then, my question is why the compiler does not accept the following
   expression?
	   char	a_array[offsetof(type,member)];
   Is it specific to our compiler?

Yes.  According to the Standard (7 Dec 1988, sec. 4.1.5, p. 99, l. 24)
the macro call "offsetof(type, member-designator)" expands to an
integral constant expression.  Since the [] expression above is
allowed to be any integral constant expression (greater than 0), your
compiler is broken.  (Do, however, make sure that you are including
<stddef.h> first, since otherwise the offsetof() call is a function
call!)

   I see that the ANSI
   C provides a macro 'offsetof' (not a compiler operator).
   It seems that the macro is expanded similarily as I have said.
   That is, ((size_t)&(((type *)0)->member)) or something like that.

Your compiler may do this, but there is no requirement in the Standard
on *how* any compiler expands offsetof(), as long as the correct
effect is obtained.  One plausable approach for a compiler is to treat
offsetof() as a "magic macro", that is, one that is recognized
by the compiler as an operator, rather than expanding it into a C code
which is then processed.

Dale Worley		Compass, Inc.			worley at compass.com
--
trump:  v.  to file for bankruptcy while living on only half a million
bucks a month.



More information about the Comp.lang.c mailing list