I need to make a "function caller" function: it receives a generic function pointer (void *
) and a variable number of arguments as arguments and it's got to call this function, passing the arguments, and return a generic pointer to the returning value. However, this entry-function pointer may point to any kind of function (with any returning type), even to functions with a constant number of arguments. It would be something like:
void * function_caller(void * function_pointer, ...) {
void * returning_value;
// Call the function and get the returning value
return returning_value; // this returning value will be properly casted outside
}
This way, the following code would work:
int function1(int a, char b) {
// ...
}
void function2(float c) {
// ...
}
float function3() {
// ...
}
int main() {
int v1;
float v3;
v1 = *(int *) function_caller((void *) &function1, 10, 'a'); // Which would be equivalent to v1 = function1(10, 'a');
function_caller((void *) &function2, 3.0); // Which would be equivalent to function2(3.0);
v3 = *(float *) function_caller((void *) &function3); // Which would be equivalent to v3 = function3();
return 0;
}
I know I'll have to use a va_list
, but I don't know how to call the function by a pointer passing the arguments.
So, guys, any idea?