← Back to C++ Course | Chapter 7: Pointers & References | Lesson 6 of 11

C++ में Pointers और Functions

Functions को pointers pass करना उन्हें original data पर सीधे काम करने देता है, एक copy की बजाय एक address सौंपने जैसा। एक function pointer आपको एक function को around pass करने भी देता है।
Syntax
cpp
void function_name(data_type *param) {
  *param = new_value;
}

function_name(&variable_name);

Functions को Pointers Pass करना

Functions को pointers pass करना function को caller की memory में पहुंचने और original variables को सीधे modify करने देता है, एक technique जिसे pass by pointer कहते हैं।

यही तरीका है जिससे functions अन्यथा arguments को value से pass करने वाली एक language में 'output parameters' simulate करते हैं।

उदाहरण: Passing Pointers to Functions

cpp
// Include std::cout and std::cin
#include <iostream>

void setValue(int *ptr, int value) {
	*ptr = value;
}

// Program execution starts in main()
int main() {
	// Declare num and set it to 0
	int num = 0;
	setValue(&num, 42);
	// Print to the console with cout
	std::cout << num << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Functions से Pointers Return करना

एक function एक pointer return कर सकता है, लेकिन आपको कभी उस function के अंदर बनाए गए एक local variable का pointer return नहीं करना चाहिए, क्योंकि local variables stack पर रहते हैं और function के return होते ही destroy हो जाते हैं।

सिर्फ static memory, heap-allocated memory, या caller के owned memory के pointers return करें।

उदाहरण: Returning Pointers from Functions

cpp
#include <iostream>

int *makeHeapValue(int value) {
	int *ptr = new int(value); // never return a pointer to a local variable
	return ptr;
}

int main() {
	int *result = makeHeapValue(10);
	std::cout << *result << std::endl;
	delete result;
	return 0;
}

Functions में Constant Pointers

आप pointer को const declare करके एक function के parameters को modification से protect कर सकते हैं, compiler और future readers दोनों को बताते हुए कि pointed-to data उस function के अंदर read-only है।

यह accidental writes को runtime पर की बजाय compile time पर पकड़ता है।

उदाहरण: Constant Pointers in Functions

cpp
#include <iostream>

void printValue(const int *ptr) {
	std::cout << *ptr << std::endl;
	// *ptr = 5; // compile error: cannot modify through a const pointer
}

int main() {
	int num = 7;
	printValue(&num);
	return 0;
}

Function Pointers

क्योंकि functions memory में variables की तरह ही addresses occupy करते हैं, C++ आपको functions के pointers declare करने देता है और उन्हें runtime पर indirectly call करने देता है, hardcode करने की बजाय program logic के आधार पर कौन सा function invoke करना है यह चुनते हुए।

यह dynamic dispatch के पीछे का low-level mechanism है।

उदाहरण: Function Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

int square(int x) {
	// Send x * x back to the caller
	return x * x;
}

// Program execution starts in main()
int main() {
	int (*funcPtr)(int) = square;
	// Print to the console with cout
	std::cout << funcPtr(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function Pointers के साथ Callback Functions

आप function pointers को दूसरे functions को arguments की तरह pass कर सकते हैं, एक general-purpose function (जैसे एक sorter या event handler) को caller द्वारा supplied custom behavior execute करने देते हुए।

यह callback pattern std::function और lambdas से पुराना है लेकिन अभी भी C-style C++ APIs में हर जगह दिखता है।

उदाहरण: Callback Functions with Function Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

void runTwice(void (*callback)()) {
	callback();
	callback();
}

void sayHi() {
	// Print to the console with cout
	std::cout << "Hi!" << std::endl;
}

// Program execution starts in main()
int main() {
	runTwice(sayHi);
	// 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. एक local variable का pointer return करना, जो function के return होने पर destroy हो जाता है, एक dangling pointer छोड़ते हुए।
  2. पहले nullptr check किए बिना एक pointer parameter dereference करना।
  3. एक function pointer का मतलब होने पर int *f(int) लिखना, जो एक pointer return करने वाला function declare करता है; एक function pointer को int (*f)(int) चाहिए।

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.