Is it possible to pass in a function pointer to a function and have that function assign the function to use?
Below is an example of what I mean:
void sub(int *a, int *b, int *c) { *c = *a + *b; } void add(int *a, int *b, int *c) { *c = *a - *b; } void switch_function(void (*pointer)(int*, int*, int*), int i) { switch(i){ case 1: pointer = &add; break; case 2: pointer = ⊂ break; } } int main() { int a; int b; int c; void (*point)(int*, int*, int*); switch_function(point, 1); // This should assign a value to `point` a = 1; b = 1; c = 0; (*point)(&a, &b, &c); return 0; } Any help is appreciated.
pointeris set to NULL on bad input. Better to crash when that pointer is called than to risk calling some actual function.