Someone can explain me, what's exactly pointers to functions?
Hmm, why we need it, and a good example to understand it?
Really thanks for anyone that can help me!
Someone can explain me, what's exactly pointers to functions?
Hmm, why we need it, and a good example to understand it?
Really thanks for anyone that can help me!
>>Someone can explain me, what's exactly pointers to functions?
Just that, a pointer that you can pass around and when you dereference it you hve a function :-)
>>Hmm, why we need it, and a good example to understand it?
A good example would be a sort function. You want to keep it as general as possible, so instead of hard coding the comparison like this
You make it more general so that the user can pass a pointer to a comparison function that you useCode:#include <stdio.h> void sort(int list[], size_t nelem) { size_t i, j; for (i = 0; i < nelem; i++) { for (j = 0; j < nelem; j++) { if (list[j] < list[j-1]) { int temp = list[j-1]; list[j-1] = list[j]; list[j] = temp; } } } } int main(void) { int i; int a[8] = {5,6,4,7,3,8,2,9}; sort(a, 8); for (i = 0; i < 8; i++) { printf("%d\n", a[i]); } return 0; }
Code:#include <stdio.h> int cmp(int a, int b) /* Return true if a is less than b */ { return a < b; } void sort(int list[], size_t nelem, int (*compare)(int, int)) { size_t i, j; for (i = 0; i < nelem; i++) { for (j = 0; j < nelem; j++) { if (compare(list[j], list[j-1])) { int temp = list[j-1]; list[j-1] = list[j]; list[j] = temp; } } } } int main(void) { int i; int a[8] = {5,6,4,7,3,8,2,9}; sort(a, 8, cmp); for (i = 0; i < 8; i++) { printf("%d\n", a[i]); } return 0; }
*Cela*
Another usefull example of using function pointers is creating tables of function pointers. For example have a structure like this:
Then you can create a table of function pointers:Code:typedef struct { int sort_method; int (*sort_fp )(int *list, size_t nr_of_elements); } fp_table_item;
The parameter INSERTION_SORT is the ID of a sorting function and insertion_sort is a sorting function which sorts a list using the insertion sort algorithm. It is prototyped as returning an int and having int *list and size_t nr_of_elements as parameters.Code:fp_table_item sort_functions_table [] = {{INSERTION_SORT, insertion_sort}, {QUICK_SORT, quick_sort}, {BUBBLE_SORT, bubble_sort}};
Now you can define a very easy handle function to handle many kinds of sorting methods.
Using tables with function pointers is readable and very well maintainable. It is especially usefull when there are a lot of functions, else you would have a large switch control structure. Not that that is bad, but I personally prefer using a table above an enormous switch control structure.Code:int handle_sorting (int sort_method, int *list, size_t nr_of_elements) { sort_functions_table [sort_method].sort_fp (list, nr_of_elements); }
Ok, I think I'm starting to get it, I'll try to run and understand the examples that you gave to me.
Thanks Cela and Shiro for your help.
I found a good site that just talk about function pointer, hell, this assunt, must be an important one
Anyway, here is the site:
http://www.function-pointer.org/CCPP/FPT/em_fpt.html
Maybe this can help people like me, to understand better.