tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: sizeof(type)



On Mon, Nov 25, 2024 at 07:53:05PM -0500, Mouse wrote:
 > There's also
 > 
 > 	sizeof (int) - 1
 > 
 > which is ambiguous even _with_ the parens; it means either
 > 	(sizeof(int))-1
 > or
 > 	sizeof((int)-1)
 > 
 > In this case, I think the ambiguity is resolved by operator precedence

C99 6.5.4 says
   cast-expression ::=
        unary-expression
      | ( type-name ) cast-expression

and 6.5.3 says
   unary-expression ::=
        postfix-expression
      | ++ unary-expression
      | -- unary-expression
      | unary-operator cast-expression
      | sizeof unary-expression
      | sizeof ( type-name )

The reason these are stratified is so that sizeof ( type-name ) is
unambiguous.

As written it also rules out ++(int)x, but that's a defect -- it is
not semantically valid but there's no reason to gratuitously make it
a syntax error. Especially since ++ +(int)x is accepted and that's
semantically equally invalid.

-- 
David A. Holland
dholland%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index