Why are these function names in parenthesis? [duplicate]

不问归期 提交于 2021-02-08 12:01:02

问题


I've been meaning to ask this question for a while now. What's going on with these functions? Why are the names in parenthesis?

void        (*think)(gentity_t *self);
void        (*reached)(gentity_t *self);    // movers call this when hitting endpoint
void        (*blocked)(gentity_t *self, gentity_t *other);
void        (*touch)(gentity_t *self, gentity_t *other, trace_t *trace);

回答1:


In your examples, the parenthesis in function name means that variable of pointing the function address. If you don't use the parenthesis

void * think(gentity_t *self);// equal (void *) think(gentity_t *self); 

It means the definition of a function with name:think, return: void *, parameter: gentity_t *self; These are the variable of the pointing the functions.




回答2:


These declarations are function pointers, which point to a function and can be changed at any time.

I suggest you do some research on function pointers in C because they are very useful.

If you know C++'s std::function then these are effectively the old C version of them.




回答3:


These are function pointers and not the function names. So they can point to any function of same type and properties.



来源:https://stackoverflow.com/questions/45746922/why-are-these-function-names-in-parenthesis

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