Pointers & Functions in C
Pointers can also store the address of a function.
This allows functions to be passed as arguments, returned from functions,
and used to create flexible menu-driven or callback-based programs.
What is a Function Pointer?
A function pointer stores the address of a function. Unlike normal pointers that point to data, these point to executable code.
- Can call a function using a pointer
- Can pass a function as an argument
- Used in event-driven and callback systems
- Used in dynamic dispatch and menu systems
Declaration of Function Pointer
return_type (*pointer_name)(parameter_list);
- *pointer_name indicates pointer to a function
- Syntax looks complex but easy with practice
Assigning Function Address
pointer_name = function_name;
A function name without parentheses represents its address.
Calling a Function Using Pointer
pointer_name(arguments); (*pointer_name)(arguments);
Both ways are valid in C.
Why Use Function Pointers?
- To pass functions as arguments
- To create callback functions
- To build menu-driven programs
- To replace long switch-case statements
- To implement dynamic behavior
Function Pointer as Argument
Functions can receive another function's address, allowing behavior changes at runtime.
void process(int a, int b, int (*op)(int,int));
This allows selecting different operations dynamically.
Array of Function Pointers
C allows storing multiple function addresses in an array. This is used for menu systems, calculators, and state machines.
int (*menu[3])(int,int);
Function Pointer in Structures
Used in device drivers, GUI frameworks, and plugin systems.
struct Actions {
void (*start)();
void (*stop)();
};
Benefits of Using Function Pointers
- Improves flexibility
- Supports modular programming
- Reduces duplicate code
- Allows dynamic behavior selection
- Used in advanced C programming (OS, embedded, drivers)
Common Mistakes
- Forgetting parentheses in pointer declaration
- Passing wrong function signature
- Incorrect number of arguments
- Confusing pointer to function vs. pointer returning function
Pointers & Functions — 10 Examples
1. Passing Pointer to a Function
#include <stdio.h>
void update(int *p){
*p = *p + 10;
}
int main(){
int x = 20;
update(&x);
printf("Updated: %d", x);
return 0;
}
2. Function Returning Pointer to Variable
#include <stdio.h>
int* getAddress(int *p){
return p;
}
int main(){
int a = 15;
int *res = getAddress(&a);
printf("%d", *res);
return 0;
}
3. Swap Using Pointer Function
#include <stdio.h>
void swap(int *a, int *b){
int t = *a;
*a = *b;
*b = t;
}
int main(){
int x = 5, y = 9;
swap(&x, &y);
printf("%d %d", x, y);
return 0;
}
4. Passing Array to Function (Pointer Form)
#include <stdio.h>
void show(int *arr){
for(int i=0; i<5; i++)
printf("%d ", arr[i]);
}
int main(){
int a[5] = {1,2,3,4,5};
show(a);
return 0;
}
5. Function Pointer Basics
#include <stdio.h>
void hello(){
printf("Hello World");
}
int main(){
void (*fp)() = hello;
fp();
return 0;
}
6. Function Pointer with Parameters
#include <stdio.h>
void sum(int a, int b){
printf("%d", a+b);
}
int main(){
void (*fp)(int,int) = sum;
fp(5,7);
return 0;
}
7. Returning Pointer from Function (Local Not Allowed)
#include <stdio.h>
int* wrong(){
int x = 10;
return &x; // invalid: returns address of local variable
}
int main(){
int *p = wrong();
printf("Undefined Behavior");
return 0;
}
8. Returning Pointer to Static Variable
#include <stdio.h>
int* value(){
static int x = 30;
return &x;
}
int main(){
int *p = value();
printf("%d", *p);
return 0;
}
9. Using Pointer to Change String via Function
#include <stdio.h>
void modify(char *s){
s[0] = 'Z';
}
int main(){
char name[] = "Sourav";
modify(name);
printf("%s", name);
return 0;
}
10. Array of Function Pointers
#include <stdio.h>
void add(){ printf("ADD\n"); }
void sub(){ printf("SUB\n"); }
void mul(){ printf("MUL\n"); }
int main(){
void (*ops[3])() = {add, sub, mul};
for(int i=0; i<3; i++)
ops[i]();
return 0;
}
Practice Questions
- What is a function pointer? Explain with syntax.
- How do you assign a function address to a pointer?
- Difference between calling a function normally and via a pointer?
- What is the purpose of callbacks?
- Write the advantages of function pointer arrays.
Practice Task
Create a menu-driven calculator using function pointers:
add(), sub(), mul(), div() stored in a function pointer array.