以下代码编译失败:
typedef int arr[10];
int main(void) {
return sizeof arr;
}
sizeof.c:3: error: expected expression before ‘arr’
但是如果我把它改成
sizeof(arr);
百事大吉。为什么?
发布于 2011-05-05 16:52:00
根据6.5.3,sizeof
有两种形式,如下:
sizeof unary-expression
sizeof ( type-name )
因为代码中的arr
是一个type-name
,所以必须用圆括号括起来。
发布于 2017-02-18 21:51:14
我想是因为你有typedef
吧。如果您删除它,它应该会编译。
来自维基百科的例子:
/* the following code fragment illustrates the use of sizeof
* with variables and expressions (no parentheses needed),
* and with type names (parentheses needed)
*/
char c;
printf("%zu,%zu\n", sizeof c, sizeof (int));
https://stackoverflow.com/questions/5894892
复制相似问题