Casting a function pointer to another type

后端 未结 7 1523
终归单人心
终归单人心 2020-11-22 08:07

Let\'s say I have a function that accepts a void (*)(void*) function pointer for use as a callback:

void do_stuff(void (*callback_fp)(void*), vo         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 08:29

    The point really isn't whether you can. The trivial solution is

    void my_callback_function(struct my_struct* arg);
    void my_callback_helper(void* pv)
    {
        my_callback_function((struct my_struct*)pv);
    }
    do_stuff(&my_callback_helper);
    

    A good compiler will only generate code for my_callback_helper if it's really needed, in which case you'd be glad it did.

提交回复
热议问题