- Pointers may also be used to point to functions
- Provides a more flexible way to call a function, by providing a choice of which function to call
- Makes it possible to pass functions to other functions
- Not extremely common, but very useful in the right situations
A function pointer is declared much like a function prototype:
int (*fp)(int x);
Here, we have declared a function pointer with the name fp
- The function it points to must take one int parameter
- The function it points to must return an int
A function pointer is initialized by setting the pointer name equal to the function name.
If we declare the following:
We can initialize the function pointer like this:
NOTE: The address of operator & is not required. When using the function name alone, it is like using an array name alone. The function name by itself represents the address (starting point) of the function in program memory.