In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.
Value of a is 10
Why do we need an extra bracket around function pointers like fun_ptr in above example?
If we remove bracket, then the expression “void (*fun_ptr)(int)” becomes “void *fun_ptr(int)” which is declaration of a function that returns void pointer. See following post for details.
How to declare a pointer to a function?
Following are some interesting facts about function pointers.
1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.
2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
3) A function’s name can also be used to get functions’ address. For example, in the below program, we have removed address operator ‘&’ in assignment. We have also changed function call by removing *, the program still works.
Value of a is 10
4) Like normal pointers, we can have an array of function pointers. Below example in point 5 shows syntax for array of pointers.
5) Function pointer can be used in place of switch case. For example, in below program, user is asked for a choice between 0 and 2 to do different tasks.
Enter Choice: 0 for add, 1 for subtract and 2 for multiply 2 Multiplication is 150
6) Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function.
For example, consider the following C program where wrapper() receives a void fun() as parameter and calls the passed function.
This point in particular is very useful in C. In C, we can use function pointers to avoid code redundancy. For example a simple qsort() function can be used to sort arrays in ascending order or descending or by any other order in case of array of structures. Not only this, with function pointers and void pointers, it is possible to use qsort for any data type.
5 10 12 15 80 90
Similar to qsort(), we can write our own functions that can be used for any data type and can do different tasks without code redundancy. Below is an example search function that can be used for any data type. In fact we can use this search function to find close elements (below a threshold) by writing a customized compare function.
Returned index is 2
The above search function can be used for any data type by writing a separate customized compare().
7) Many object oriented features in C++ are implemented using function pointers in C. For example virtual functions. Class methods are another example implemented using function pointers. Refer this book for more details.
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Difference between const char *p, char * const p and const char * const p
- Pointer to an Array | Array Pointer
- Double Pointer (Pointer to Pointer) in C
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Dangling, Void , Null and Wild Pointers
- NULL pointer in C
- What are near, far and huge pointers?
- What are the data types for which it is not possible to create an array?
- Generic Linked List in C
- void pointer in C
- Difference between pointer and array in C?
- Comparator function of qsort() in C
- Pointer vs Array in C
- Why C treats array parameters as pointers?
- How to declare a pointer to a function?