Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof((type)) is a constraint violation?

Reply
Thread Tools

sizeof((type)) is a constraint violation?

 
 
Noob
Guest
Posts: n/a
 
      03-07-2012
Hello,

While debugging the output of a configure script, I came across
the following program, which is refused by gcc.

int main(void)
{
int res = sizeof((int));
return res;
}

$ gcc -Wall -std=c89 -pedantic mini.c
mini.c: In function 'main':
mini.c:3:25: error: expected expression before ')' token

Obviously, the compiler dislikes the double parenthesis
around a type-name.

Does this mean ((int)) cannot be takes as a unary-expression?
However ((x)) or ((42)) are valid instances of unary-expression?

http://www.cs.dartmouth.edu/~mckeema...otation/c.html

unary-expression
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

Regards.
 
Reply With Quote
 
 
 
 
Nils M Holm
Guest
Posts: n/a
 
      03-07-2012
Noob <root@127.0.0.1> wrote:
> Obviously, the compiler dislikes the double parenthesis
> around a type-name.
>
> Does this mean ((int)) cannot be takes as a unary-expression?


((int)) is not a parenthesized primary expression, it is a
parenthesized type cast, which is not a sentence that can
be produced by the ANSI C grammar (as specified in The C
Programming Language, 2nd Ed).

((int) x) would be a primary expression and would be accepted
as an operand of 'sizeof'. More parens around the expression can
be added to suit your taste.

> However ((x)) or ((42)) are valid instances of unary-expression?


These, on the other hand, /are/ primary expressions.

--
Nils M Holm < n m h @ t 3 x . o r g > www.t3x.org
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-07-2012
Noob <root@127.0.0.1> writes:
> While debugging the output of a configure script, I came across
> the following program, which is refused by gcc.
>
> int main(void)
> {
> int res = sizeof((int));
> return res;
> }
>
> $ gcc -Wall -std=c89 -pedantic mini.c
> mini.c: In function 'main':
> mini.c:3:25: error: expected expression before ')' token
>
> Obviously, the compiler dislikes the double parenthesis
> around a type-name.

[...]

Parentheses are heavily overloaded (used for different syntactic
purposes) in C. They can be arbitrary nested in some cases, but not in
others.

For example, the parentheses in

x * (y + z)

create a parenthesized expression. Any expression or subexpression,
even if it's already a parenthesized expression, can be enclosed in
parentheses; the only effect is to make it a primary expression if it
wasn't one already. So this:

x * ((y + z))

is also valid.

Parentheses are also used to surround function arguments:

func(x, y)

These do *not* create a parenthesized expression. Surrounding them with
a second set of parentheses doesn't create a two-level parenthesized
expression. If you write:

func((x, y))

then that changes the meaning of the inner parentheses which now *do*
form a parenthesized expression) and of the comma (which is now a comma
operator, not a delimiter between function arguments).

Yet another use of parentheses is in a sizeof operator applied to a type
name:

sizeof (int)

These do not create a parenthesized expression, and they're *not*
directly analagous to the parentheses in a function call. Instead,
they're an inherent part of the syntax of a unary-expression:

sizeof ( type-name )

Just as with the parentheses in a function call, surrounding them with a
second set of parentheses:

sizeof ((int))

doesn't create a more deeply nested parenthesized expression,
since there was no parenthesized expression in the first place.
Instead, in this case, it's a syntax error.

A cast expression is another distinct use of parentheses in the language
grammar:

( type-name ) cast-expression

Although the parenthesized type-name in "sizeof (int)" and the parenthesized
expression in "(int)expr" are visually similar, and probably deliberately so,
they result from different grammar productions and are logically unrelated.
The "(int)" prefix in "(int)expr" is informally referred to as a cast operator.
The "(int)" in "sizeof (int)" is *not* a cast operator, it's a distinct
case of a parenthesized type name.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-07-2012
Nils M Holm <> writes:
> Noob <root@127.0.0.1> wrote:
>> Obviously, the compiler dislikes the double parenthesis
>> around a type-name.
>>
>> Does this mean ((int)) cannot be takes as a unary-expression?

>
> ((int)) is not a parenthesized primary expression, it is a
> parenthesized type cast, which is not a sentence that can
> be produced by the ANSI C grammar (as specified in The C
> Programming Language, 2nd Ed).

[...]

The "(int)" in "sizeof (int)" is not a "type cast". In fact, there's no
such thing as a "type cast" in the C grammar.

"(int) 42" is a cast-expression (C11 6.5.4), and the "(int)" prefix is
informally referred to as a "cast operator".

But the "(int)" in "sizeof (int)" is not a cast operator; it's merely
part of the syntax of a unary-expression (C11 6.5.3):

sizeof ( type-name )

The parenthesized type-name in a cast-expression and the parenthesized
type-name in a sizeof expression are visually similar, and that's
probably not a coincidence, but in terms of the language grammar they're
logically unrelated. (In both cases, there was a need to have a
type-name in parentheses as part of an expression, and wrapping it in
parentheses was an easy way to do that while avoiding ambiguity.)

Strictly speaking, then "((int))" in "sizeof ((int))" is not anything at
all; since it's a syntax error, you can't meaningfully say that it's any
particular syntactic entity. For all we know, "int" could have been a
misspelling of the identifier "hint".

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Nils M Holm
Guest
Posts: n/a
 
      03-08-2012
Keith Thompson <kst-> wrote:
> But the "(int)" in "sizeof (int)" is not a cast operator; it's merely
> part of the syntax of a unary-expression (C11 6.5.3):
>
> sizeof ( type-name )


Indeed. I thought that the parens belong to a cast operator that form
an operand of 'sizeof', but they are just part of the 'sizeof' syntax.

> Strictly speaking, then "((int))" in "sizeof ((int))" is not anything at
> all; [...]


Exactly my point.

--
Nils M Holm < n m h @ t 3 x . o r g > www.t3x.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re-using a simple type definition; with enumeration constraint andwithout enumeration constraint puvit82 XML 4 02-01-2008 03:46 PM
CLOCK__SIGNAL constraint! pls help Alistair Lamb VHDL 1 03-24-2005 01:58 PM
foreignKey constraint error. GMK ASP .Net 1 03-11-2005 07:45 AM
Re: How to change Read Only Constraint to Read-Write Isaac VHDL 0 07-10-2003 01:43 PM
Can Not Access Foreign Key Constraint Rajesh Tiwari ASP .Net 0 06-30-2003 02:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57