Reputation: 3952
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
Upvotes: 5
Views: 2028
Reputation: 57774
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.
Upvotes: 1