C++ में Pointers और Functions
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक local variable का pointer return करना, जो function के return होने पर destroy हो जाता है, एक dangling pointer छोड़ते हुए।
- पहले
nullptrcheck किए बिना एक pointer parameter dereference करना। - एक function pointer का मतलब होने पर
int *f(int)लिखना, जो एक pointer return करने वाला function declare करता है; एक function pointer कोint (*f)(int)चाहिए।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: