Pointer to Functions
Understand function pointers by learning how to declare and assign them to functions with matching signatures, and call functions indirectly through them.
We'll cover the following...
We'll cover the following...
So far, we have used pointers to store the address of variables. However, in C, functions also have addresses. Just like variables, functions occupy memory, and their starting address can be stored in a pointer.
A function pointer allows a program to refer to a function indirectly. Instead of calling a function by name, we can store its address in a variable and call it through that variable. This makes programs more flexible. It allows functions to be:
Passed as arguments to other functions.
Selected at runtime.
Used to implement callback behavior.
Declaring a function pointer
Consider a simple function: