sizeof(((struct_type *)0)->member_name)

Wayne Throop throopw at dg_rtp.UUCP
Tue Dec 9 02:02:52 AEST 1986


> henry at utzoo.UUCP (Henry Spencer)

>> Can I do something like this:
>>     char a[sizeof(struct name *)0->element];

> Don't think so.

Why not?  Note what H&S have to say about sizeof, on page 153

    Applying the sizeof operator to an expression yields the same result
    as if it had been applied to the name of the type of the expression.
    [...]
    When sizeof is applied to an expression, the expression is analyzed
    at compile time to determine its type, but the expression itself is
    not compiled into executable code.

And draft X3J11, in section 3.3.3.4:

    The size is determined from the type of the operand, which is not
    itself evaluated.

Note that the evaluation of an indirection of the null pointer is indeed
illegal.  But that isn't what is going on here.

So again... why is sizeof(((struct_type *)0)->member_name) illegal?  I'm
not saying it IS legal, mind you (I don't want to be in the unenviable
position of disagreeing with Henry on a matter of C semantics).  I just
don't see any reason why it isn't.

--
A LISP programmer knows the value of everything, but the cost of nothing.
                                --- Alan J. Perlis
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list