Function pointer to different functions with different arguments in C
The cleanest way to do it is to use a union:
typedef union {
double (*func_one)(double x, double a, double b, double c);
double (*func_two)(double x, double p[], double c);
} func_one_two;
Then you can initialize an instance of the union, and include information to the swap_function
function to say which field is valid:
func_one_two func;
if (condition_1)
func.func_one = my_func_one;
else if (condition_2)
func.func_two = my_func_two;
// The function that will use the function I passed to it
swap_function(a, b, func, condition_1);
This assumes that swap_function
can know based on condition_1
being false
that it should assume condition_2
. Note that the union is passed by value; it's only a function pointer in size after all so that's not more expensive than passing a pointer to it.
My question is, for this scenario, Can I at all define a function pointer?
No. (Other than by dirty typecasting.)
Is there any other way to do this?
Your best bet is to create a wrapper function for one of your existing functions. For example:
double my_func_one_wrapper(double x, double p[], double c) {
return my_func_one(x, p[0], p[1], c);
}
That way, you have two functions with the same signature, and therefore the same function-pointer type.
An old topic but I am facing same issue and finally came with this idea.
If you want the same prototype for each functions you can wrap the parameters in structure like this :
typedef struct {
double a,
b,
c;
}chunk1_t;
typedef struct {
double p[];
double c;
}chunk2_t;
Then your function pointer becomes:
double (*pfunc) (double x, void *args);
which leads to something like this :
pfunc cb;
double swap_function(double x, pfunc cb, void *args);
double my_func_one(double x, void *args) {
chunk1_t *chunk = (chunk1_t*) args;
return x + chunk->a + chunk->b + chunk->c;
}
double my_func_two(double x, void *args) {
chunk2_t *chunk = (chunk2_t*) args;
return x + chunk->p[0] + chunk->p[1] + chunk->c ;
}
int main(){
// declare a, b,...
double a = 1.0f;
//...
// cast for safety
chunk1_t myChunk1 = {(double)a, (double)b, (double)c};
// don't if you like risk
chunk2_t myChunk2 = {p, c};
swap_function(x, cb, &myChunk1);
swap_function(x, cb, &myChunk2);
}
Using function pointer stored in structure:
#define FUNC_ONE_METHOD 1
#define FUNC_TWO_METHOD 2
typedef struct chunk{
double a, b, c;
double p[];
int method;
double (*pfunc) (double x, struct chunk *self);
}chunk_t;
double swap_function(double x, chunk_t *ch){
switch (ch->method)
{
case FUNC_TWO_METHOD:
ch->pfunc = my_func_two;
break;
case FUNC_ONE_METHOD:
ch->pfunc = my_func_one;
break;
default:
return -1; // or throw error
return ch->pfunc(x, ch);
}
chunk c = {.a= 1, .b=3, .c=1, .method=1};
swap_function(x, &c);