# The relevance of pointer to funtions

• 02-27-2005
caroundw5h
The relevance of pointer to funtions
I've been searching google for some info on this but I won't be making a finite state machine any time soon and I haven't come up where I would need to assign a function as a parameter for another function. So the question is: what other uses for pointer to functions are there and have any of you used it? Are there any optimization benefits in using them?
thanks.
• 02-27-2005
Dave_Sinkula
Quote:

Originally Posted by caroundw5h
So the question is: what other uses for pointer to functions are there and have any of you used it?

Comparing functions.
• 02-27-2005
... if youve searcher with google you should have come across

www.function-pointer.org

anyway: YES!!! function pointers are VERY useful.
for example when some function requires some cost-function.
Code:

```int some_algorithm( struct State, (*pfn_cost)(int, int)) {   if(pfn_cost(blah1, blah2) < pfn_cos(blah1, blah2) {     ...   }   ... }```
if you have different cost functions you just have to pass a cost function to this - instead of writing multiple versions of your algorithm.

also: you can map values to function

a byte code interpreter might use this mapping - depending on the byte value the appropriate action is performed.

(note that a switch statement would be faster but uglier here - as long as the compiler creates a jump table)

Code:

```void f1() {   printf("hi, im function 1\n"); } void f2() {   printf("hi, im function 2\n"); } void (*(a_pfn_my_map[2]))(); a_pfn_my_map[0] = &f1; a_pfn_my_map[1] = &f2; int i = blah; if(i >= 0 && i <= 2)   a_pfn_my_map[i]();```

another useful thing about function pointers is that you might have function which all do the same - but the implementation differs.

for example matrix multiplication can be done using the ordinary instruction set, or 3dnow or mmx extension or whatever.

consider that:
Code:

```  if(true == mmx_support)     matrix_mult_mmx(...);   else if(true == 3dnow_support)     matrix_mult_3dnow(...);   else     matrix_mult_fallback(...);```
your code would have hundrets of conditionals whenever youd call matrix multiply

```Matrix (*pfn_matrix_multiply)(Matrix *p_m1, Matrix *p_m2);   if(true == mmx_support)     pfn_matrix_multiply = &matrix_mult_mmx;   else if(true == 3dnow_support)     pfn_matrix_multiply = &matrix_mult_3dnow;   else     pfn_matrix_multiply = matrix_mult_fallback;   // now we can always use the fastest implementation without having to worry which implementation actually "hides" behind the pointer   pfn_matrix_multiply(...);```
```#include <stdio.h> #include <math.h> double twice(double x); double half(double x); double thrice(double x); void showmenu(void); #define NUM 4 int main(void) {     double (*pf[NUM])(double)  = {twice, half, thrice, sqrt};     double val;     double ans;     int sel;         printf("Enter a number (negative to quit): ");     while (scanf("%lf", &val) && val >= 0)     {         showmenu();         while (scanf("%d", &sel) && sel >= 0 && sel <= 3)         {             //ans = (*pf[sel])(val);             ans = pf[sel](val);//BSD way             printf("answer = %f\n", ans);             showmenu();         }         printf("Enter next number (negative to quit): ");             }        puts("bye");     return 0; } void showmenu(void) {     puts("Enter one of the following choices:");     puts("0) double the value        1) halve the value");     puts("2) triple the value        3) squareroot the value");     puts("4) next number"); } double twice(double x) {return 2.0 * x;} double half(double x) {return x / 2.0;} double thrice(double x) {return 3.0 * x;}```