← Back to C Course | Chapter 12: Advanced Topics | Lesson 6 of 20

C के Function Pointers

एक function pointer एक function का address रखता है, एक recipe कहाँ है यह बताने वाले एक note की तरह। फिर आप इसके जरिए function call कर सकते हैं या इसे pass कर सकते हैं।
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक function pointer का मतलब होने पर int *f(int) लिखना, जो एक pointer return करने वाला function declare करता है; इसे int (*f)(int) होना चाहिए।
  2. एक NULL function pointer के जरिए call करना, जो crash करता है।
  3. एक अलग signature वाला function pointer को assign करना, जो call होने पर undefined behavior देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.