为什么sizeof的int是错误的,同时的sizeof(int)的是正确的? [英] Why sizeof int is wrong, while sizeof(int) is right?

查看:244
本文介绍了为什么sizeof的int是错误的,同时的sizeof(int)的是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,的sizeof 是用于计算任何数据类型和前pression的大小操作,当操作数是前pression,括号可以省略。

We know that sizeof is an operator used for calculating the size of any datatype and expression, and when the operand is an expression, the parentheses can be omitted.

int main()
{
        int a;

        sizeof int;
        sizeof( int );
        sizeof a;
        sizeof( a );

        return 0;
}

的sizeof的第一种用法是错误的,而有些则是正确的。

the first usage of sizeof is wrong, while others are right.

当它被使用gcc编译,将得到下面的错误信息:

When it is compiled using gcc, the following error message will be given:

main.c:5:9: error: expected expression before ‘int’

我的问题是,为什么C标准不允许这种操作。请问的sizeof INT 病因任何含糊之处?

推荐答案

下面可能是模糊的:

sizeof int * + 1

(sizeof的为(int *))+ 1 (的sizeof(INT))*(+1)

显然,C语言可能出台一个规则来解决歧义,但我能想象,为什么它没有打扰。有了,因为它代表的语言,类型说明符从来没有在一个前pression出现裸体,所以没有必要对规则来解决是否是第二个 * 是类型或算术运算器的一部分。

Obviously the C language could have introduced a rule to resolve the ambiguity, but I can imagine why it didn't bother. With the language as it stands, a type specifier never appears "naked" in an expression, and so there is no need for rules to resolve whether that second * is part of the type or an arithmetic operator.

现有的语法并已解决的潜在的不确定性的sizeof(INT *)+ 1 。这是(sizeof的为(int *))+ 1 的sizeof(为(int *)(+ 1))

The existing grammar does already resolve the potential ambiguity of sizeof (int *) + 1. It is (sizeof(int*))+1, not sizeof((int*)(+1)).

C ++有几分相似的问题,以解决函数样式转换语法。你可以写 INT(0),你可以写的typedef INT *的IntPtr;的IntPtr(0); ,但你不能写为int *(0)。在这种情况下,分辨率是,裸类型必须是简单类型的名字,它不能只是可能有它的空间,或尾随标点符号任何旧的类型ID。也许的sizeof 本来可以用相同的限制性规定,我不能确定。

C++ has a somewhat similar issue to resolve with function-style cast syntax. You can write int(0) and you can write typedef int *intptr; intptr(0);, but you can't write int*(0). In that case, the resolution is that the "naked" type must be a simple type name, it can't just be any old type id that might have spaces in it, or trailing punctuation. Maybe sizeof could have been defined with the same restriction, I'm not certain.

这篇关于为什么sizeof的int是错误的,同时的sizeof(int)的是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆