为什么结果是:
sizeof(function_name)?
1
?社区里有人说sizeof(main)
是 1 但 sizeof(main())
是 4。这里 main
正在返回int
,所以它返回 4。但是如果我们声明为 char main()
而不是int main()
它只会返回 1。
最佳答案
在函数上使用 sizeof
是非法的。
C99 §6.5.3.4 The
sizeof
operatorThe
sizeof
operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.
因此,如果 foo
是函数名称,则 sizeof(foo)
是非法的,但 sizeof(foo())
适用
。 foo()
返回值上的 sizeofmain()
在标准 C 中返回 int
,在当今大多数机器上它是 4 个字节。
关于c - 为什么 sizeof(function_name) 是 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21936951/