Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function pointer to different functions with different arguments in C

I have two functions with variable number and types of arguments

double my_func_one(double x, double a, double b, double c) { return x + a + b + c }
double my_func_two(double x, double p[], double c) { return x + p[0] + p[1] + c }

I want to use a pointer to a function to the functions I defined above based on a some condition getting true e.g.

if (condition_1)
   pfunc = my_func_one;
else if (condition_2)
   pfunc = my_func_two;

 // The function that will use the function I passed to it
 swap_function(a, b, pfunc);

My question is, for this scenario, Can I at all define a function pointer? If yes, how?
My understanding is that the prototype of function pointer should be the same for all those functions it CAN be pointed to.

typedef double (*pfunction)(int, int);

In my case they are not the same. Is there any other way to do this?

Language

I am developing in C and I am using gcc 4.4.3 compiler/linker

like image 525
fahad Avatar asked May 27 '13 09:05

fahad


People also ask

How do you pass a function as an argument to another function in C?

We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.

How can a function pointer be an argument?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

Is comparison of function pointer possible in C?

Yes, it is fine. The C standard is pretty self-explanatory in this case (C11 6.3.

What is a function pointer in C?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.


1 Answers

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.

like image 112
unwind Avatar answered Oct 11 '22 12:10

unwind