Cast to function pointer?

2020-02-23 06:35发布

Have come across the line of code shown below I think it may be a cast to a function pointer that returns void and takes a void pointer, is that correct?

(void (*)(void *))SGENT_1_calc

4条回答
贼婆χ
2楼-- · 2020-02-23 07:15

Yes it is a cast as you have stated.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-23 07:21

Yes it is, the function should be looking like this

void func(void*);

But the statement is missing a target, since a cast to nothing is useless. So it should be like

func = (void (*)(void *))SGENT_1_calc;
查看更多
等我变得足够好
4楼-- · 2020-02-23 07:27

Yes it is correct. I find that not very readable, so I suggest declaring the signature of the function to be pointed:

 typedef void sigrout_t(void*);

I also have the coding convention that types ending with rout_t are such types for functions signatures. You might name it otherwise, since _t is a suffix reserved by Posix

latter on I am casting, perhaps to call it like

 ((sigrout_t*) SGENT_1_calc) (someptr);
查看更多
家丑人穷心不美
5楼-- · 2020-02-23 07:30

yes its a function pointer which you can assign to a function with proto void funcname(void*) Here the SGENT_1_calc can be directly assigned to funcname

查看更多
登录 后发表回答