Explain void (*signal(int signo, void *(func)(int)))(int)

自闭症网瘾萝莉.ら 提交于 2019-12-03 13:48:54

问题


Please explain this type signature : void (*signal(int signo, void *(func)(int)))(int)


回答1:


The type signature of the signal function is a bit more clear when a typedef is used for the function pointers that are passed around:

typedef void (*sighandler_t)(int);
sighandler_t signal(int signo, sighandler_t func);

sighandler_t is a pointer to a function that takes an int parameter and returns nothing. The signal function takes such a function pointer as its second parameter. It also returns a function pointer of that type.




回答2:


C declarations need to be read from the inside out. The tricky part with complex function declarations is figuring out which is the innermost declarator (where to start). Its generally the first identifier that is not a type identifier. So in this case:

void (*signal(int signo, void *(func)(int)))(int)

the declarator is signal. Within the parenthesis, suffixes are higher precedence than prefixes, so signal is a function taking two args (the (int signo, void *(func)(int)) part), that returns a pointer (the prefix *) to a function taking a single int arg (the (int) on the end), and returning void



来源:https://stackoverflow.com/questions/2270713/explain-void-signalint-signo-void-funcintint

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