我发现很奇怪,在使用 sizeof int
时出现编译错误,而 sizeof var
(其中 var 是一个变量)工作正常。
int a;
a = (int) sizeof( a ); //ok
a = (int) sizeof( int ); //ok
a = (int) sizeof a; //ok
a = (int) sizeof int; //error
谁能告诉我,为什么会出现错误?
(旧机器,mac osx 10.5.8,gcc 4.0.1)
最佳答案
根据 C 标准(引用自 ISO/IEC 9899:TC3 第 6.5.3.4 节):
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.
因此使用不带括号的类型名称是不合法的。
此外,sizeof
返回类型为 size_t
的实现定义值,您可能不应该转换它。
关于c - 数据类型的 sizeof 运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782604/