C Function Pointer
1. What is a Function Pointer?
-
A function pointer is a pointer that points to the address of a function instead of a variable.
-
It allows you to call a function indirectly and is useful for callbacks, dynamic dispatch, and tables of functions.
2. Syntax of Function Pointer
Example:
-
funcPtr→ pointer to a function that takes twointparameters and returnsint.
3. Example: Basic Function Pointer
Output:
You can also write
funcPtr = add;without&, andfuncPtr(5,10)works fine.
4. Function Pointer as Callback
-
You can pass a function pointer as an argument to another function.
Output:
Function pointers are widely used in callbacks and event-driven programming.
5. Array of Function Pointers
-
You can store multiple function pointers in an array:
Output:
6. Key Points About Function Pointers
-
Declared as:
return_type (*pointer_name)(parameter_list);. -
Can point to any function with matching signature.
-
Used to call functions indirectly.
-
Useful for callbacks, dynamic function selection, and event handling.
-
Can be stored in arrays for tables of functions.
-
Can pass as function arguments to other functions.
