Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C Pointers · 03 Jul 2026 · Hindi + English

C में Function Pointer Example के साथ: Syntax, Callback और Table

Function pointer किसी function का address store करता है जिससे उसे indirectly call कर सकते हैं. Syntax, callback example और function table वाला calculator सीखें.

What is a function pointer?

A function pointer stores the address of a function, letting you call the function indirectly — through the pointer. Just as data lives at addresses, a function's compiled code also lives at an address in memory.

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*                  */
The parentheses around (*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;
}
Direct : 7 Pointer : 7

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;
}
1 4 9 16 2 4 6 8

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;
}
Add(10,3) = 13 Sub(10,3) = 7 Mul(10,3) = 30

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 क्या है?

Function pointer किसी function का address store करता है, जिससे आप function को indirectly — pointer के through — call कर सकते हैं. जैसे data addresses पर रहता है, वैसे ही function का compiled code भी memory में एक address पर रहता है.

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;
}
Direct : 7 Pointer : 7

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;
}
1 4 9 16 2 4 6 8

एक 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;
}
Add(10,3) = 13 Sub(10,3) = 7 Mul(10,3) = 30

Function pointers का array लंबी if/else chains की जगह लेता है — index से operation चुनें. Menu-driven programs और embedded command tables यही pattern लगातार use करते हैं.

Frequently Asked Questions

C में function pointer क्या है?

Function pointer वह variable है जो function का address store करता है, return_type (*name)(params) से declare होता है, और indirect calls व callbacks की सुविधा देता है.

int (*fp)(int) में parentheses क्यों ज़रूरी हैं?

उनके बिना int *fp(int) एक ऐसा function declare करता है जो int* return करता है; parentheses * को fp से bind करके उसे function का pointer बनाते हैं.

Callback function क्या है?

Callback वह function है जो argument के रूप में (function pointer से) pass होता है ताकि receive करने वाला function उसे call कर सके — qsort comparisons और event handlers इसी mechanism पर चलते हैं.