取地址的main()函数是非法的吗? [英] Is it illegal to take address of main() function?
问题描述
根据此答案 使用函数main()是非法的(§3.6.1.3 )如果其名称出现在潜在求值的表达式(§3.2)中,则使用函数。
According to this answer using function main() is illegal (§3.6.1.3) and a function is used if its name appears in a potentially evaluated expression (§3.2).
假设我有这样的代码: p>
Suppose I have this code:
printf( "%p", &main );
其中函数的名称 main()
in which name of function main()
appears in expression &main
.
上述代码是否会违法?
推荐答案
是的。当你引用时,标准说你不能使用
main
。
Yes. As you quote, the standard says that you cannot use
main
.
请注意,函数的地址不匹配%p
。
相应的参数必须有类型 void *
;任何其他
类型(除了 char *
)是非法的,并导致未定义的
行为。
Note too that the address of a function does not match "%p"
.
The corresponding argument must have type void*
; any other
type (except maybe char*
) is illegal, and results in undefined
behavior.
这篇关于取地址的main()函数是非法的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!