Loading content...
Loading content...
A Function Pointer is a pointer that stores the address of a function instead of the address of a variable.
Using function pointers, we can call a function indirectly through its memory address. Function pointers are commonly used in callback functions, event-driven programming, and dynamic function execution.
Function pointers provide flexibility in programs by allowing functions to be selected and executed at runtime.
Common uses include:
Callback functions
Event handling
Function tables
Dynamic operation selection
Implementing polymorphic behavior
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int (*fptr)(int, int);
fptr = &add;
printf("%d", fptr(10, 5));
return 0;
}15In this example, the function pointer fptr stores the address of the add() function and calls it dynamically.
A function pointer must match the return type and parameter list of the function it points to.
return_type (*pointer_name)(parameter_types);int (*fptr)(int, int);This declaration means:
Function returns an integer.
Function accepts two integer parameters.
fptr stores the address of such a function.
After declaration, assign the address of a function.
plaintext
plaintext
pointer_name = &function_name;or simply
plaintext
pointer_name = function_name;plaintext
fptr = add;or
plaintext
fptr = &add;Both are valid.
Once the function address is stored, the function can be called through the pointer.
plaintext
printf("%d", fptr(10, 5));Output:
plaintext
15Store the address of a function.
Must match the function signature exactly.
Can point to different functions with the same signature.
Cannot perform arithmetic operations like normal pointers.
Useful for dynamic function execution.
Can be stored inside arrays and structures.
One of the most important uses of function pointers is passing functions as arguments to other functions.
plaintext
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
void calc(int a, int b, int (*operation)(int, int))
{
printf("%d\n", operation(a, b));
}
int main()
{
calc(10, 5, add);
calc(10, 5, subtract);
return 0;
}plaintext
15
5First call uses add().
Second call uses subtract().
The function to execute is decided at runtime.
This technique is known as a callback function.
C structures cannot directly contain functions, but they can contain function pointers.
plaintext
#include <stdio.h>
typedef struct
{
int width;
int height;
int (*area)(int, int);
} Rectangle;
int calculateArea(int w, int h)
{
return w * h;
}
int main()
{
Rectangle rect;
rect.width = 10;
rect.height = 5;
rect.area = calculateArea;
printf("Area = %d",
rect.area(rect.width, rect.height));
return 0;
}plaintext
Area = 50This technique is often used to mimic object-oriented behavior in C.
Multiple function pointers can be stored inside an array.
plaintext
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int main()
{
int (*operations[])(int, int) =
{
add,
sub,
mul
};
printf("%d\n", operations[0](10, 5));
printf("%d\n", operations[1](10, 5));
printf("%d\n", operations[2](10, 5));
return 0;
}plaintext
15
5
50Stores the address of a variable.
plaintext
int num = 10;
int *ptr = #Stores the address of a function.
plaintext
int (*fptr)(int, int) = add;Enable callback functions.
Allow dynamic function execution.
Reduce repetitive code.
Useful in event-driven programming.
Help implement function tables.
Increase program flexibility.
Syntax can be difficult for beginners.
Code becomes harder to read if overused.
Wrong function signatures can cause errors.
Debugging can become more complex.
A function pointer stores the address of a function.
It must match the return type and parameter list of the target function.
Functions can be called dynamically through function pointers.
Function pointers are commonly used for callback functions.
They can be stored inside structures and arrays.
Function pointers make programs more flexible and reusable.