I have trouble seeing the utility of function pointers. I guess it may be useful in some cases (they exist, after all), but I can't think of a case where it's better or unavoidable to use a function pointer.
Could you give some example of good use of function pointers (in C or C++)?
In C, the classic use is the qsort function, where the fourth parameter is pointer to a function to use to perform the ordering within the sort. In C++, one would tend to use functors (objects that look like functions) for this kind of thing.
Function pointers can be used in C to create an interface against which to program. Depending on the specific functionality that is needed at runtime, a different implementation can be assigned to the function pointer.
I used function pointers recently to create an abstraction layer.
I have a program written in pure C that runs on embedded systems. It supports multiple hardware variants. Depending on the hardware I am running on, it needs to call different versions of some functions.
At initialization time, the program figures out what hardware it is running on and populates the function pointers. All of the higher-level routines in the program just call the functions referenced by pointers. I can add support for new hardware variants without touching the higher-level routines.
I used to use switch/case statements to select the proper function versions, but this became impractical as the program grew to support more and more hardware variants. I had to add case statements all over the place.
I also tried intermediate function layers to figure out which function to use, but they didn't help much. I still had to update case statements in multiple places whenever we added a new variant. With the function pointers, I only have to change the initialization function.
Use of function pointer
To call function dynamically based on user input. By creating a map of string and function pointer in this case.
This way we have used function pointer in our actual company code. You may write 'n' number of function and call them using this method.
OUTPUT:
Well, I generally use them (professionally) in jump tables (see also this StackOverflow question).
Jump tables are commonly (but not exclusively) used in finite state machines to make them data driven. Instead of nested switch/case
you can make a 2d array or function pointers and just call
handleEvent[state][event]
Most examples boil down to callbacks: You call a function
f()
passing the address of another functiong()
, andf()
callsg()
for some specific task. If you passf()
the address ofh()
instead, thenf()
will call backh()
instead.Basically, this is a way to parametrize a function: Some part of its behavior is not hard-coded into
f()
, but into the callback function. Callers can makef()
behave differently by passing different callback functions. A classic isqsort()
from the C standard library that takes its sorting criterion as a pointer to a comparison function.In C++, this is often done using function objects (also called functors). These are objects that overload the function call operator, so you can call them as if they were a function. Example:
The idea behind this is that, unlike a function pointer, a function object can carry not only an algorithm, but also data:
Another advantage is that it is sometimes easier to inline calls to function objects than calls through function pointers. This is a reason why sorting in C++ is sometimes faster than sorting in C.