Function Pointers in C
What is a Function Pointer?
Section titled “What is a Function Pointer?”A function pointer is a pointer that stores the address of a function instead of the address of a variable.
Just as a pointer to an int stores the address of an int variable, a function pointer stores the address of a function.
Why Use Function Pointers?
Section titled “Why Use Function Pointers?”- Pass functions as arguments to other functions (Callbacks)
- Select a function at runtime
- Implement menus and dispatch tables
- Used extensively in operating systems, device drivers, and libraries like
qsort()
General Syntax
Section titled “General Syntax”return_type (*pointer_name)(parameter_list);Example:
int (*pf)(int, int);Meaning:
pfis a pointer.- It points to a function.
- The function takes two
intarguments. - The function returns an
int.
Understanding the Syntax
Section titled “Understanding the Syntax”int (*pf)(int, int);Read inside-out:
(*pf) → pf is a pointer(int, int) → points to a function taking two int argumentsint → function returns intDeclaration Examples
Section titled “Declaration Examples”void (*p1)();Pointer to a function returning void.
int (*p2)(int);Pointer to a function taking one int and returning int.
float (*p3)(float, float);Pointer to a function taking two floats and returning float.
char (*p4)(char);Pointer to a function returning char.
Defining a Function
Section titled “Defining a Function”#include<stdio.h>
int add(int a, int b){ return a + b;}Assigning a Function to a Pointer
Section titled “Assigning a Function to a Pointer”int (*pf)(int, int);
pf = add;or
pf = &add;Both are correct because the function name automatically converts to its address.
Calling Through a Function Pointer
Section titled “Calling Through a Function Pointer”Method
int result = pf(10, 20);Method 2
int result = (*pf)(10, 20);Both are equivalent.
Complete Example
Section titled “Complete Example”#include<stdio.h>
int add(int a, int b){ return a + b;}
int main(){ int (*pf)(int, int);
pf = add;
printf("%d", pf(10,20));
return 0;}Output
30Function Name is a Pointer
Section titled “Function Name is a Pointer”Suppose
int add(int, int);Then
addmeans
Address of add()Therefore,
pf = add;is equivalent to
pf = &add;Function Pointer vs Normal Pointer
Section titled “Function Pointer vs Normal Pointer”| Normal Pointer | Function Pointer |
|---|---|
| Stores address of a variable | Stores address of a function |
int *p; | int (*pf)(int,int); |
p = &x; | pf = add; |
*p gives value | pf() calls the function |
Difference Between These Declarations
Section titled “Difference Between These Declarations”1. Pointer to Function
int (*pf)(int, int);pf is a pointer to a function.
2. Function Returning Pointer
int *pf(int, int);pf is a function taking two int arguments and returning a pointer to int.
2. Pointer to Integer
int *p;p points to an integer variable.
Function Returning Function?
int pf(int)(int); // Invalid. ❌A function cannot return another function. It may return a pointer to a function.
Array of Function Pointers
Section titled “Array of Function Pointers”int (*arr[3])(int, int);Meaning:
arris an array of 3 function pointers- Each pointer points to a function taking two
ints and returning anint.
Example:
arr[0] = add;arr[1] = sub;arr[2] = mul;
printf("%d", arr[1](20,10));Function Pointer as Parameter
Section titled “Function Pointer as Parameter”void calculate(int a, int b, int (*operation)(int,int)){ printf("%d", operation(a,b));}Usage
calculate(10,20,add);This is called a callback function.
Returning a Function Pointer
Section titled “Returning a Function Pointer”int add(int,int);int sub(int,int);
int (*select(int choice))(int,int){ if(choice==1) return add; else return sub;}Spiral (Inside-Out) Rule
Section titled “Spiral (Inside-Out) Rule”To read complicated declarations:
- Start from the variable name.
- Move right if possible.
- Move left.
- Resolve parentheses first.
Example
int (*pf)(int,int);pf↓(*pf) → pointer↓(int,int) → to function↓int → returning intKey Points
Section titled “Key Points”-
A function pointer stores the address of a function.
-
General syntax:
return_type (*pointer_name)(parameters); -
addand&addare equivalent when assigning to a function pointer. -
Functions can be called using
pf()or(*pf)(). -
A function cannot return another function, but it can return a pointer to a function.
-
Arrays of function pointers and function pointers as parameters are widely used for callbacks and dispatch tables.
-
Parentheses around
*pointer_nameare mandatory. Without them, the declaration has a completely different meaning.# Function Pointers in C