Call a void* as a function without declaring a function pointer

后端 未结 3 555
深忆病人
深忆病人 2021-02-05 15:32

I\'ve searched but couldn\'t find any results (my terminology may be off) so forgive me if this has been asked before.

I was wondering if there is an easy way to call a

3条回答
  •  不知归路
    2021-02-05 16:33

    Your cast should be:

    ((void (*)(void)) ptr)();
    

    In general, this can be made simpler by creating a typedef for the function pointer type:

    typedef void (*func_type)(void);
    ((func_type) ptr)();
    

    I should, however, point out that casting an ordinary pointer (pointer to object) to or from a function pointer is not strictly legal in standard C (although it is a common extension).

提交回复
热议问题