🔴 Advanced  ·  Lesson 35

Function Pointers

Function pointer क्या है?

आप जानते हैं कि pointer किसी variable का address रख सकता है। हैरानी वाली बात यह है: functions भी memory में addresses पर रहते हैं, इसलिए pointer किसी function का address भी रख सकता है। यही function pointer है।

यह क्यों चाहिए? क्योंकि इससे आपका program run time पर तय कर सकता है कि कौन सा function call करना है, functions को arrays में रख सकता है, और व्यवहार दूसरे functions में भेज सकता है। यह callbacks और लचीले, plugin-जैसे code की नींव है।

Syntax, टुकड़ों में

Declaration पहली बार अजीब लगता है। इसे तीन हिस्सों में पढ़ें:

C Language
int (*fp)(int, int);
//  ^     ^
//  |     दो parameter types
//  pointer नाम, ( ) में लिपटा
// "int" return type है

ऐसे पढ़ें: "fp ऐसे function का pointer है जो दो int लेता है और int लौटाता है।" *fp के आसपास parentheses ही इसे function pointer बनाते हैं, न कि pointer लौटाने वाला function।

⚠️ Parentheses न छोड़ें

int (*fp)(int) = function का pointer।   int *fp(int) = int * लौटाने वाला function। पूरी तरह अलग!

Pointer से function call करना

Function pointer को किसी असली function पर point कराएँ (बस उसका नाम इस्तेमाल करें), फिर call करें।

C Language
#include <stdio.h>
int add(int a, int b) { return a + b; }

int main() {
    int (*fp)(int, int) = add;   // add पर point करें
    printf("Sum = %d\n", fp(3, 4));   // fp से call करें
    return 0;
}
Output:
Sum = 7

किसी function का नाम अकेले उसका address होता है, इसलिए fp = add सीधे चलता है। फिर fp(3, 4) pointer के ज़रिए add call करता है।

Callbacks: व्यवहार भेजना

चूँकि function pointer बस एक value है, आप इसे किसी दूसरे function में भेज सकते हैं। पाने वाला जो आपने दिया उसे "call back" करता है — यही callback है।

C Language
#include <stdio.h>
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

void compute(int x, int y, int (*op)(int, int)) {
    printf("Result = %d\n", op(x, y));   // जो भेजा उसे call करें
}
int main() {
    compute(4, 5, add);   // Result = 9
    compute(4, 5, mul);   // Result = 20
    return 0;
}
Output:
Result = 9
Result = 20

वही compute function अलग व्यवहार करता है इस पर निर्भर कि आप कौन सा function देते हैं। यही लचीलापन पूरी बात है।

Function pointers का array (menu)

कई functions को array में रखें और index से एक चुनें — बड़े switch के बिना menu के लिए एकदम सही।

C Language
#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 };
    int choice = 2;   // 0=add, 1=sub, 2=mul
    printf("Answer = %d\n", ops[choice](6, 3));
    return 0;
}
Output:
Answer = 18

Index 2 चुनने पर mul call होता है, जो 6 × 3 = 18 देता है। choice बदलें तो बिना किसी if-else के अलग operation चलेगा।

Function pointers क्यों ज़रूरी हैं

  • Callbacks — libraries को आपका code call करने देते हैं (event handlers, sorting के लिए comparison functions)।
  • Menus / dispatch tables — लंबे switches के बजाय number से function चुनना।
  • Plugins — caller बदले बिना run time पर व्यवहार बदलना।

आम गलतियाँ

  • *fp के आसपास parentheses भूलना, जिससे अर्थ पूरी तरह बदल जाता है।
  • Pointer के signature को असली function के parameters या return type से मेल न खिलाना
  • ऐसा function pointer call करना जो कभी assign ही नहीं हुआ (अब भी NULL या garbage)।
  • fp (pointer) और fp() (उसके ज़रिए call) में उलझना।
🏋️ अभ्यास

एक छोटा calculator बनाएँ: +, -, ×, ÷ के लिए चार function pointers का array, और user को number से operation चुनने दें। चुने function को दो inputs पर call करके result print करें।

सारांश

  • Function pointer किसी function का address रखता है, ताकि आप उसे अप्रत्यक्ष रूप से call कर सकें।
  • इसे returnType (*name)(paramTypes) के रूप में declare करें — *name के आसपास parentheses ज़रूरी हैं।
  • इसे function का नाम देकर assign करें, फिर name(args) से call करें।
  • इसे किसी दूसरे function में भेजना एक callback बनाता है।
  • Function pointers का array साफ़ menus और dispatch tables बनाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में function pointer क्या है?
Function pointer एक pointer है जो किसी variable के बजाय किसी function का address रखता है। जैसे सामान्य pointer आपको किसी value तक पहुँचाता है, function pointer आपको उसके ज़रिए function call करने देता है — और run time पर यह भी तय करने देता है कि कौन सा function call करना है।
C में function pointer कैसे declare करते हैं?
पहले return type लिखें, फिर parentheses में * के साथ pointer नाम, फिर parameter types। जैसे int (*fp)(int, int); fp को ऐसे function का pointer घोषित करता है जो दो ints लेता है और int लौटाता है। *fp के आसपास parentheses अनिवार्य हैं।
C में callback क्या है?
Callback वह function है जो function pointer के ज़रिए किसी दूसरे function में argument के रूप में भेजा जाता है, ताकि पाने वाला function उसे वापस call कर सके। इससे व्यवहार customise होता है — जैसे किसी sorting routine को अलग-अलग comparison functions भेजना ताकि वह अलग तरीकों से sort करे।
Function pointer declaration में parentheses क्यों मायने रखते हैं?
क्योंकि int *fp(int) का मतलब है "int * लौटाने वाला function", जबकि int (*fp)(int) का मतलब है "int लौटाने वाले function का pointer"। *fp के आसपास parentheses अर्थ पूरी तरह बदल देते हैं, इसलिए इन्हें छोड़ना एक classic गलती है।
Function pointers का array किसके लिए इस्तेमाल होता है?
Function pointers का array आपको index से function चुनने देता है, जो menus और command tables के लिए एकदम सही है। if-else या switch की लंबी श्रृंखला के बजाय, आप functions को array में रखते हैं और user के चुने एक को एक indexed call से call करते हैं।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.