Is passing additional parameters through function pointer legal/defined in C? [duplicate]

我的未来我决定 提交于 2019-11-30 21:23:50

The behavior of your program is undefined. The fact that it compiles at all is because of the cast, which effectively tells the compiler "this is wrong, but do it anyway". If you remove the cast, you'll get the appropriate error message:

a.c:17:8: error: assignment from incompatible pointer type [-Werror]

(From gcc -Wall -Werror.)

More specifically, the behavior depends on the calling conventions. If you were on a platform were the arguments were passed in "reverse" order on the stack, the program would give a very different result.

It is undefined behavior. Use at your own risk. It has been rumored to cause Nasal Demons!

The function call is undefined behavior.

(C99, 6.3.2.3p8) "[...] If a converted pointer is used to call a function whose type is not compatible with the pointed-to type, the behavior is undefined."

For information note that a function type:

(C99, 6.2.5p20) "[...] describes a function with specified return type. A function type is characterized by its return type and the number and types of its parameters."

ChrisW

Whether it works will depend on the calling convention being used.

I wouldn't recommend it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!