C++ में Reference और Pointer का अंतर
In this page:
data_type *pointer_name = &variable_name; // can be reassigned or nullptr
data_type &reference_name = variable_name; // must be initialized, cannot be reseated
Syntax Differences
Pointers declaration और dereferencing के लिए एक asterisk (*) और एक variable का address retrieve करने के लिए एक ampersand (&) इस्तेमाल करते हैं, जबकि references declaration पर सिर्फ ampersand इस्तेमाल करते हैं और फिर exactly एक normal variable जैसे behave करते हैं, बाद में किसी special dereference syntax की ज़रूरत नहीं।
उदाहरण: Syntax Differences
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 5
int value = 5;
int *ptr = &value;
int &ref = value;
// Print to the console with cout
std::cout << *ptr << " " << ref << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nullability
Pointers को एक empty या not-yet-initialized state represent करने के लिए nullptr assign किया जा सकता है, जो optional या lazily-set data के लिए essential है।
References null नहीं हो सकते — उन्हें बनते ही एक valid variable से bind होना ज़रूरी है, जो null-pointer bugs की एक पूरी category rule out करता है।
उदाहरण: Nullability
#include <iostream>
int main() {
int *ptr = nullptr; // valid: represents "no value"
int value = 5;
int &ref = value; // references must always refer to something
std::cout << ref << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reassignment
Pointers को उनकी lifetime के दौरान किसी भी समय एक अलग variable की ओर point करने के लिए reassign किया जा सकता है, उन्हें iteration और dynamic data structures के लिए suitable बनाते हुए।
References initialization पर अपने target से bind होते हैं और बाद में कभी किसी और चीज़ को refer करने के लिए redirect नहीं किए जा सकते।
उदाहरण: Reassignment
#include <iostream>
int main() {
int a = 1, b = 2;
int *ptr = &a;
ptr = &b; // pointers can be reassigned
std::cout << *ptr << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pass-by-Value बनाम Reference बनाम Pointer
Pass-by-value argument को पूरी तरह copy करता है, जो safe है लेकिन बड़े objects के लिए expensive हो सकता है। Pass-by-pointer सिर्फ memory address copy करता है लेकिन function को nullptr check करना ज़रूरी बनाता है।
Pass-by-reference clean syntax के साथ सीधे original variable share करता है और कोई null-check burden नहीं।
उदाहरण: Pass-by-Value vs Reference vs Pointer
// Include std::cout and std::cin
#include <iostream>
void byValue(int x) { x = 100; }
void byReference(int &x) { x = 100; }
void byPointer(int *x) { *x = 100; }
// Program execution starts in main()
int main() {
int a = 1, b = 1, c = 1;
byValue(a);
byReference(b);
byPointer(&c);
// Print to the console with cout
std::cout << a << " " << b << " " << c << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Use Cases
Clean function parameters और operator overloading के लिए default से references इस्तेमाल करें, जहाँ syntax naturally पढ़ता है।
इसकी बजाय pointers इस्तेमाल करें जब आपको nullptr से एक optional/absent value represent करनी हो, pointer arithmetic perform करनी हो, या खुद heap पर allocate की गई memory manage करनी हो।
उदाहरण: Use Cases
#include <iostream>
void printValue(const int &x) { // reference: clean parameter syntax
std::cout << x << std::endl;
}
int main() {
int arr[] = {1, 2, 3};
int *ptr = arr; // pointer: needed for array iteration
printValue(*ptr);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक reference को null बनाने की कोशिश करना, जो allowed नहीं है।
- एक reference को किसी दूसरे variable की ओर point करने के लिए reassign करना।
- एक reference जैसे behave करने की उम्मीद करते हुए एक pointer को null check करना भूल जाना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: