C के Function Pointers
In this page:
return_type (*pointer_name)(parameter_types) = function_name;
pointer_name(arguments);
Function Pointer क्या है?
एक function pointer एक data value की बजाय एक function का memory address store करता है, आपको सीधे इसके नाम से नहीं बल्कि pointer के जरिए indirectly उस function को call करने देते हुए।
यह ऐसा code लिखने की foundation है जो choice को hardcode करने की बजाय runtime पर decide कर सके कि कौन सा function चलाना है।
उदाहरण: What is a Function Pointer?
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
int (*funcPtr)(int) = square;
// Print formatted text to the screen
printf("%d", funcPtr(5));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Pointers का Syntax
एक function pointer declare करने के लिए pointer नाम और इसके asterisk को parentheses में wrap करना ज़रूरी है, जैसे int (*compare)(int, int);, क्योंकि parentheses के बिना compiler इसकी बजाय इसे एक pointer return करने वाला function parse करेगा।
यह syntax सही करना function pointers के साथ पहली बार काम करते समय सबसे common stumbling block है।
उदाहरण: Syntax of Function Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
int compare(int a, int b) {
// Send a - b back to the caller
return a - b;
}
// Program execution starts in main()
int main() {
int (*compareFn)(int, int) = compare;
// Print formatted text to the screen
printf("%d", compareFn(5, 3));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions को Function Pointers Pass करना
एक argument के तौर पर एक function pointer pass करना एक general-purpose function को यह जाने बिना caller-supplied logic में वापस call करने देता है कि वह logic actually क्या करता है, exactly इसी तरह standard library का qsort() आपको आपके दिए comparison function इस्तेमाल करके कोई भी data sort करने देता है।
यह general function को कई अलग-अलग use cases में reusable रखता है।
उदाहरण: Passing Function Pointers to Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) { return n * n; }
void apply(int (*fn)(int), int value) {
// Print formatted text to the screen
printf("%d", fn(value));
}
// Program execution starts in main()
int main() {
apply(square, 4);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Pointers का Array
एक array में function pointers store करना एक jump table बनाता है, जहाँ एक index select करना यह चुनता है कि कौन सा function चलता है — menus, state machines, या opcode dispatch implement करने का एक common pattern if/else statements की लंबी chain के बिना।
हर array element बस एक function address है, इसलिए table[choice]() call करना सीधे सही handler invoke करता है।
उदाहरण: Array of Function Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
// Program execution starts in main()
int main() {
int (*ops[2])(int, int) = {add, sub};
// Print formatted text to the screen
printf("%d", ops[0](3, 4));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Pointers के साथ Common Mistakes
एक function pointer का declared signature (इसका return type और parameter types) exactly उस function से match होना चाहिए जिसकी ओर यह point करने के लिए assigned है; एक mismatched signature assign करना C में compile होता है लेकिन call होने पर undefined behavior produce करता है।
हमेशा double-check करें कि एक callback का declared type उस function से match करता है जिसे आप actually register कर रहे हैं।
उदाहरण: Common Mistakes with Function Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) { return n * n; }
// Program execution starts in main()
int main() {
int (*correctPtr)(int) = square;
// Print formatted text to the screen
printf("%d", correctPtr(5));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक function pointer का मतलब होने पर
int *f(int)लिखना, जो एक pointer return करने वाला function declare करता है; इसेint (*f)(int)होना चाहिए। - एक
NULLfunction pointer के जरिए call करना, जो crash करता है। - एक अलग signature वाला function pointer को assign करना, जो call होने पर undefined behavior देता है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions