worley@compass.com (Dale Worley) (05/21/91)
In article <1285@unisql.UUCP> pckim@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@compass.com
--
trump: v. to file for bankruptcy while living on only half a million
bucks a month.
steve@taumet.com (Stephen Clamage) (05/21/91)
pckim@unisql.UUCP (Pyung-Chul Kim) writes: |But, I found that our compiler (in SunOS 4.1) evaluate the expression |in compile time by trying to 'cc -S test.c' (the test.s contains |numeric literal for the expression). |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? ANSI requires that the 'offsetof' macro expand to an integral constant expression of type 'size_t'. Such an expression is certainly allowed to specifiy the size of an array. The standard Sun compilers are not ANSI-conformant, and this appears to be yet another place where they fail to conform. -- Steve Clamage, TauMetric Corp, steve@taumet.com