# Thread: The relevance of pointer to funtions

1. ## 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.

2. 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.

3. ... 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

Code:
```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(...);```

is it clearer now?

4. Nice!! the array of pointers to functions is nice!! Bit manipulation is where my book is taking me next perhaps once i've done that, i might see some other uses for pointers to functions. As it stands i just play around with previous code and muck around like this:
Code:
```#include <stdio.h>
#include <math.h>

double twice(double x);
double half(double x);
double thrice(double x);
#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)
{
while (scanf("%d", &sel) && sel >= 0 && sel <= 3)
{
//ans = (*pf[sel])(val);
ans = pf[sel](val);//BSD way
}
printf("Enter next number (negative to quit): ");

}
puts("bye");
return 0;
}