5

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
4
  • Looks fine here: ideone.com/uqLLR5. Commented Apr 27, 2014 at 22:32
  • Very strange, your code doesn't work for me, like my old one: compiles fine but arguments are not the ones expected. I'm using Visual Studio 2013 Commented Apr 27, 2014 at 22:35
  • Out of curiosity, what values is Oli's code displaying? Commented Apr 27, 2014 at 22:37
  • @LoveMetal, Please include a segment of code that implements 'va_list'. Commented Apr 27, 2014 at 22:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.