Function Pointer in C with Example: Syntax, Callback and Table
A function pointer stores the address of a function so you can call it indirectly. Learn the syntax, a callback example and a calculator using a function table.
What is a function pointer?
Syntax (the parentheses matter)
return_type (*pointer_name)(parameter_types);
int (*fp)(int, int); /* pointer to a function taking (int,int), returning int */
int *fp(int, int); /* DIFFERENT: a function returning int* */
(*fp) are mandatory. Without them, you declare a function that returns a pointer — the single most common syntax mistake with function pointers.First example
#include <stdio.h>
int add(int a, int b) { return a + b; }
int main() {
int (*fp)(int, int); /* declare */
fp = add; /* assign (also: fp = &add;) */
printf("Direct : %d\n", add(3, 4));
printf("Pointer : %d\n", fp(3, 4)); /* also: (*fp)(3,4) */
return 0;
}
fp = add and fp = &add are equivalent; fp(3,4) and (*fp)(3,4) are equivalent. The function name, like an array name, decays to its address.
Real use 1: Callback functions
#include <stdio.h>
void process(int arr[], int n, void (*callback)(int)) {
for (int i = 0; i < n; i++)
callback(arr[i]); /* caller decides what happens */
}
void printSquare(int x) { printf("%d ", x * x); }
void printDouble(int x) { printf("%d ", x * 2); }
int main() {
int data[4] = {1, 2, 3, 4};
process(data, 4, printSquare); printf("\n");
process(data, 4, printDouble); printf("\n");
return 0;
}
One process() function, two behaviours — the caller injects the logic. This is exactly how qsort() accepts your comparison function.
Real use 2: A calculator with a function table
#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 (*ops[3])(int,int) = {add, sub, mul}; /* array of function pointers */
char *names[3] = {"Add", "Sub", "Mul"};
for (int i = 0; i < 3; i++)
printf("%s(10,3) = %d\n", names[i], ops[i](10, 3));
return 0;
}
An array of function pointers replaces long if/else chains — pick the operation by index. Menu-driven programs and embedded command tables use this pattern constantly.
Function pointer क्या है?
Syntax (parentheses ज़रूरी हैं)
return_type (*pointer_name)(parameter_types);
int (*fp)(int, int); /* (int,int) लेने वाले, int return करने वाले function का pointer */
int *fp(int, int); /* अलग चीज़: int* return करने वाला function */
(*fp) के around parentheses mandatory हैं. उनके बिना आप pointer return करने वाला function declare कर देते हैं — function pointers की सबसे common syntax mistake.पहला Example
#include <stdio.h>
int add(int a, int b) { return a + b; }
int main() {
int (*fp)(int, int); /* declare */
fp = add; /* assign (fp = &add; भी सही) */
printf("Direct : %d\n", add(3, 4));
printf("Pointer : %d\n", fp(3, 4)); /* (*fp)(3,4) भी सही */
return 0;
}
fp = add और fp = &add equivalent हैं; fp(3,4) और (*fp)(3,4) equivalent हैं. Function name, array name की तरह, अपने address में decay होता है.
Real use 1: Callback functions
#include <stdio.h>
void process(int arr[], int n, void (*callback)(int)) {
for (int i = 0; i < n; i++)
callback(arr[i]); /* क्या होगा, caller तय करता है */
}
void printSquare(int x) { printf("%d ", x * x); }
void printDouble(int x) { printf("%d ", x * 2); }
int main() {
int data[4] = {1, 2, 3, 4};
process(data, 4, printSquare); printf("\n");
process(data, 4, printDouble); printf("\n");
return 0;
}
एक process() function, दो behaviours — logic caller inject करता है. qsort() आपका comparison function ठीक ऐसे ही accept करता है.
Real use 2: Function table वाला calculator
#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 (*ops[3])(int,int) = {add, sub, mul}; /* function pointers का array */
char *names[3] = {"Add", "Sub", "Mul"};
for (int i = 0; i < 3; i++)
printf("%s(10,3) = %d\n", names[i], ops[i](10, 3));
return 0;
}
Function pointers का array लंबी if/else chains की जगह लेता है — index से operation चुनें. Menu-driven programs और embedded command tables यही pattern लगातार use करते हैं.
Frequently Asked Questions
What is a function pointer in C?
A function pointer is a variable that stores the address of a function, declared as return_type (*name)(params), allowing indirect calls and callbacks.
Why are parentheses needed in int (*fp)(int)?
Without them, int *fp(int) declares a function returning int*; the parentheses bind * to fp, making it a pointer to a function.
What is a callback function?
A callback is a function passed as an argument (via a function pointer) so the receiving function can call it — the mechanism behind qsort comparisons and event handlers.