Josh Morrison Josh Morrison -4 years ago 147
C Question

Figuring out obscure pointer typedef

typedef solution_type (*algorithm_ptr_type) (
problem_type problem,
void (*post_evaluation_callback)(void *move, int score)/* = NULL*/

Please help me! Thank you

Answer Source

This means, algorithm_ptr_type is a pointer to a function returning solution_type and whose parameters are:

  • problem of type problem_type
  • post_evaluation_callback which is again a function pointer taking two parameters (void* and int) , and returning void.

And the same can be written as (easy and readable syntax):

typedef  void (*callback_type)(void *move, int score);

typedef solution_type (*algorithm_type)(problem_type, callback_type);

Note: the name of the parameters are optional, so I removed it, to make the typedef short and cute!

In C++11, this can be simplified further as follows:

//first define a utility to make function pointer.
template<typename Return, typename ... Parameters>
using make_fn = Return (*)(Paramaters...);

then use it as,

using callback_type = make_fn<void, void*, int>;

using algorithm_type = make_fn<solution_type, problem_type, callback_type>;

Here the first argument to make_fn is the return type, and the rest are the parameters — easy to decipher each one!


solution_type SomeFunction(problem_type problem, callback post_evaluation)

   //call the callback function
   post_evaluation(arg1, arg2);

algorithm_ptr_type function = SomeFunction;

//call the function
function(arg, someOtherFunction);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download