{char foo[]="This is a test."; sizeof(foo);}

Wayne A. Throop throopw at xyzzy.UUCP
Tue Apr 19 01:07:44 AEST 1988


> scjones at sdrc.UUCP (Larry Jones)
>> gwyn at brl-smoke.ARPA (Doug Gwyn )
>>> major at eleazar.Dartmouth.EDU (Lou Major)
>>>char foo[]="This is a test.";
>>>sizeof (foo) == sizeof (char *)
>> Since when?  [...foo has 16 bytes...]
>> (I don't think the array name is turned into a pointer just because it's
>> surrounded by parentheses.)
> If it ain't, the compiler's broke!  The sizeof operator can be applied to a
> parenthesized type name or to an expression.  Since "foo" isn't a type name,
> the operand of sizeof is an expression.  When an array name appears in an
> expression and it's not the operand of & or sizeof (whose operand is the
> parenthesized express, remember), it's converted into a pointer to the first
> element.

Well, this isn't so in either K&R C or draft X3J11 C.  The mistake seems
to be about what the parenthesized expression does, which is:

        draft X3J11 section 3.3.1
        A parenthisized expression is a primary expression.  Its type
        and value are identical to those of the unparenthesized
        expression.  It is an lvalue, a function designator, or a void
        expression if the unparenthesized expression is, respectively,
        an lvalue, a function designator, or a void expression.

Under draft X3J11, array names are unmodifiable lvalues of array type.
Now, granted, elsewhere the standard says that expressions of array type
are converted to expressions of pointer type except in case of & or
sizeof.  On the surface, this seems to be a contradiction, but it is
not.  Parenthesis are not an operator... they are one of the primary
expressions, and the &-and-sizeof enumeration is only a listing of
*operators* where the conversion-of-array-to-pointer is suppressed.

I admit that this could be made clearer in the standard.

K&R say similar things about parenthesis, to whit:

        K&R 2nd edition, appendix A7, page 201
        A parenthesized expression is a primary expression whose type
        and value are identical to those of the unadorned expression.
        The presence of parentheses does not affect whether the
        expression is an lvalue.

... and the first edition says something similar, though I don't have it
to hand just now.  

--
"Classic": A book which people praise and don't read.
                                --- Mark Twain
-- 
Wayne Throop      <the-known-world>!mcnc!rti!xyzzy!throopw



More information about the Comp.lang.c mailing list