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?
I am developing in C and I am using gcc 4.4.3 compiler/linker
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.
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.
Yes, it is fine. The C standard is pretty self-explanatory in this case (C11 6.3.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With