Re: sizeof(x)



On Sun, 30 Mar 2008 23:07:05 +0300, Ioannis Vranos wrote:
Harald van Dijk wrote:
On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
In C99 it is mentioned:

"The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type.".

If I am not wrong, this implies that

int x;

size_t y= sizeof(x);


is not valid.

You are; (x) is a perfectly valid expression, so there's no problem
taking the size of (x).

I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

That doesn't say sizeof(x) is invalid any more than the standard does.

Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.

Yes, it does. Look at the grammar.

unary-expression:
sizeof unary-expression

unary-expression:
postfix-expression

postfix-expression:
primary-expression

primary-expression:
( expression )

A parenthesised expression is a primary-expression, which is a postfix-
expression, which is a unary-expression, which is a valid operand of
sizeof.

The standard doesn't explicitly mention that parenthesised expressions
are valid operands of +, -, *, /, ^, &, or pretty much any other
operator. The grammar makes that clear already.
.



Relevant Pages

  • Re: Implementing
    ... "The sizeof operator yields the size of its operand, ... rjh at above domain ...
    (comp.lang.c)
  • Re: Is cast operator unary or binary? How many operands?
    ... sizeof unary-expression ... sizeof (type-name) ... operand, and the parentheses of a cast have to go around the operand. ...
    (comp.lang.c)
  • Re: Regarding sizeof Operator
    ... "the operand of a sizeof operator is usually not evaluated" ... Although sizeof must evaluate the size of a variable length array under c99 ... "If the type of the operand is a variable length array type, ...
    (comp.lang.c)
  • Re: Sizeof question
    ... |> | sizeof returns the number of chars that array occupies. ... | On the most common system, a char is 1 byte though. ... operand, which may be an expression or the parenthesized ... operand is a variable length array type, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: sizeof() style question
    ... The sizeof operator yields the size of its ... operand, which may be an expression or the parenthesized ... Some people, for reasons known only to them, insist on parenthesizing the expression, but ... Consider a simple situation frequently asked about here: how do you determine the number of elements in an array declared as an array in the present scope? ...
    (comp.lang.c)