Function Pointer Tutorials
Programming
Function Pointer in C
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.
Why Use Function Pointers?
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
Example of Function Pointer
#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;
}Output
15In this example, the function pointer fptr stores the address of the add() function and calls it dynamically.
Function Pointer Declaration
A function pointer must match the return type and parameter list of the function it points to.
Syntax
return_type (*pointer_name)(parameter_types);Example
int (*fptr)(int, int);This declaration means:
Function returns an integer.
Function accepts two integer parameters.
fptrstores the address of such a function.
Function Pointer Initialization
After declaration, assign the address of a function.
Syntax
plaintext
plaintext
pointer_name = &function_name;or simply
plaintext
pointer_name = function_name;Example
plaintext
fptr = add;or
plaintext
fptr = &add;Both are valid.
Calling a Function Using Function Pointer
Once the function address is stored, the function can be called through the pointer.
Example
plaintext
printf("%d", fptr(10, 5));Output:
plaintext
15Properties of Function Pointers
Store 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.
Function Pointer as Function Argument (Callback Function)
One of the most important uses of function pointers is passing functions as arguments to other functions.
Example
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;
}Output
plaintext
15
5How It Works
First call uses
add().Second call uses
subtract().The function to execute is decided at runtime.
This technique is known as a callback function.
Function Pointers in Structures
C structures cannot directly contain functions, but they can contain function pointers.
Example
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;
}Output
plaintext
Area = 50This technique is often used to mimic object-oriented behavior in C.
Array of Function Pointers
Multiple function pointers can be stored inside an array.
Example
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;
}Output
plaintext
15
5
50Difference Between Normal Pointer and Function Pointer
Normal Pointer
Stores the address of a variable.
plaintext
int num = 10;
int *ptr = #Function Pointer
Stores the address of a function.
plaintext
int (*fptr)(int, int) = add;Advantages of Function Pointers
Enable callback functions.
Allow dynamic function execution.
Reduce repetitive code.
Useful in event-driven programming.
Help implement function tables.
Increase program flexibility.
Disadvantages of Function Pointers
Syntax can be difficult for beginners.
Code becomes harder to read if overused.
Wrong function signatures can cause errors.
Debugging can become more complex.
Summary
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.