How to use varargs in conjunction with function pointers in C on Win64?

后端 未结 2 425
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 02:14

Consider the following C program:

#include 
#include 

typedef void callptr();

static void fixed(void *something, double val)         


        
相关标签:
2条回答
  • 2020-12-18 02:51

    You should work with consistent function definitions, even if that means to use varargs even if not needed. The best is to be as verbose as needed.

    ...

    typedef void myfunc_t(void *, ...);
    

    ...

    myfunc_t dynamic;
    void dynamic(void * something, ...)
    {
    

    ...

    }
    

    ...

    int main()
    {
        double x = 1337.1337;
        myfunc_t *callnow;
        callnow = &dynamic;
        callnow(NULL, x);
    
        printf("%f\n", x);
    }
    
    0 讨论(0)
  • 2020-12-18 02:56

    Your code is not valid. Calling a variadic function requires a prototype indicating that it's variadic, and the function pointer type you're using does not provide this. In order for the call not to invoke undefined behavior, you would have to cast the dynamic_func pointer like this to make the call:

    ((void (*)(void *, ...))dynamic_func)(NULL, x);
    
    0 讨论(0)
提交回复
热议问题