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

C++ में Reference और Pointer का अंतर

References और pointers दोनों आपको दूसरे variable तक पहुंचने देते हैं, लेकिन एक reference एक permanent nickname है जबकि एक pointer एक note है जिसे कहीं और point करने के लिए बदला जा सकता है। यह topic compare करता है कि कब कौन इस्तेमाल करें।
Syntax
cpp
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

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

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

cpp
#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;
}

Reassignment

Pointers को उनकी lifetime के दौरान किसी भी समय एक अलग variable की ओर point करने के लिए reassign किया जा सकता है, उन्हें iteration और dynamic data structures के लिए suitable बनाते हुए।

References initialization पर अपने target से bind होते हैं और बाद में कभी किसी और चीज़ को refer करने के लिए redirect नहीं किए जा सकते।

उदाहरण: Reassignment

cpp
#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;
}

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

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

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

cpp
#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;
}
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. एक reference को null बनाने की कोशिश करना, जो allowed नहीं है।
  2. एक reference को किसी दूसरे variable की ओर point करने के लिए reassign करना।
  3. एक reference जैसे behave करने की उम्मीद करते हुए एक pointer को null check करना भूल जाना।

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.