comp.lang.c
[Top] [All Lists]

Re: sizeof(x)

Subject: Re: sizeofx
From: Harald van Dijk
Date: Sun, 30 Mar 2008 22:13:14 +0200
Newsgroups: comp.lang.c

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.

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy