offsetof()

++Don; mccrady at torolab6.vnet.ibm.com
Tue May 21 22:36:37 AEST 1991


> That is, ((size_t)&(((type *)0)->member)) or something like that.
> I had thought the expression is evaluated at run-time because
> the following expression is not accepted:
>         char    a_array[offsetof(type,member)];

If offsetof() is defined as you say, then its use in the array index
is illegal according to ANSI.  ANSI requires that the array dimension
be an integral constant expression.  Section 3.5 states:

      Cast operators in an integral constant expression shall
      only convert arithmetic types to integral types, except
      as part of an operand to the sizeof operator.

Your compiler's definition of offsetof() converts pointer types to
integral types, and thus violates the above constraint.

I know that's not very helpful to you, but it at least answers your
question.

++Don;



More information about the Comp.lang.c mailing list