Pointer to functions
Function Pointers in C
One of the powerful features of C is the ability to define pointers to functions.
-
A function pointer is a variable that stores the address of a function.
-
Like other pointers, function pointers can be declared, assigned, and used to call the function they point to.
Declaring Function Pointers
The general syntax for declaring a function pointer is:
Example:
-
fpis a function pointer that points to a function taking one float and one char argument and returns an int.
Initializing Function Pointers
-
Function pointers must be initialized before use.
-
Assign the function’s name (without parentheses) to the pointer:
Example: Using Function Pointers
Output:
Key Points:
-
Function pointers allow dynamic selection of functions at runtime.
-
Useful for implementing callback functions and table-driven programs.
-
Can point to any function with a matching signature (return type and parameters).
Comments
Post a Comment