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 पहली बार अजीब लगता है। इसे तीन हिस्सों में पढ़ें:
int (*fp)(int, int); // ^ ^ // | दो parameter types // pointer नाम, ( ) में लिपटा // "int" return type है
ऐसे पढ़ें: "fp ऐसे function का pointer है जो दो int लेता है और int लौटाता है।" *fp के आसपास parentheses ही इसे function pointer बनाते हैं, न कि pointer लौटाने वाला function।
int (*fp)(int) = function का pointer। int *fp(int) = int * लौटाने वाला function। पूरी तरह अलग!
Pointer से function call करना
Function pointer को किसी असली function पर point कराएँ (बस उसका नाम इस्तेमाल करें), फिर call करें।
#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;
}Sum = 7
किसी function का नाम अकेले उसका address होता है, इसलिए fp = add सीधे चलता है। फिर fp(3, 4) pointer के ज़रिए add call करता है।
Callbacks: व्यवहार भेजना
चूँकि function pointer बस एक value है, आप इसे किसी दूसरे function में भेज सकते हैं। पाने वाला जो आपने दिया उसे "call back" करता है — यही callback है।
#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;
}Result = 9
Result = 20
वही compute function अलग व्यवहार करता है इस पर निर्भर कि आप कौन सा function देते हैं। यही लचीलापन पूरी बात है।
Function pointers का array (menu)
कई functions को array में रखें और index से एक चुनें — बड़े switch के बिना menu के लिए एकदम सही।
#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;
}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 क्या है?
C में function pointer कैसे declare करते हैं?
* के साथ pointer नाम, फिर parameter types। जैसे int (*fp)(int, int); fp को ऐसे function का pointer घोषित करता है जो दो ints लेता है और int लौटाता है। *fp के आसपास parentheses अनिवार्य हैं।C में callback क्या है?
Function pointer declaration में parentheses क्यों मायने रखते हैं?
int *fp(int) का मतलब है "int * लौटाने वाला function", जबकि int (*fp)(int) का मतलब है "int लौटाने वाले function का pointer"। *fp के आसपास parentheses अर्थ पूरी तरह बदल देते हैं, इसलिए इन्हें छोड़ना एक classic गलती है।