简体   繁体   中英

Pointer of a function with variable number of arguments

I have a function with the following prototype:

void func(int an, ...);

And I would like to store the adress of this function and call it later. I have really no idea to how to do that, I desesperatly tried :

void (*funcPtr)(int, ...);  // Declaration
funcPtr = func;     // Storage
(*funcPtr)(3,2,5);      // Call

This code compiles fine, but at execution it does crap, when I enter my function the arguments in my va_list are not the ones I sent.

Thanks in advance

EDIT : Alright, I just forgot the first argument. In my code above, the call line should be replaced with:

(*funcPtr)(3,3,2,5);        // Call

Functions are pointers naturally. So you can simply call:

funcPtr(3,3,2,5);

It looks like you have everything right. If the function does not have variable arguments, it is a really good idea to declare the function pointer with the right "shape" of arguments for protection from passing malformed arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM