Reference vs Pointer in C++ (Alias vs Address)
A reference is a permanent second name for an existing variable; a pointer is a separate variable holding an address. 6 differences with swap function proof.
The nickname analogy
int aman = 100;
int &monu = aman; // REFERENCE: second name for the SAME variable
int *slip = &aman; // POINTER : separate variable holding an address
monu = 150; // change through nickname -> aman itself changes
cout << aman; // 150
The 3 rules that make references different
int a = 10, b = 20;
// RULE 1: must be initialized at birth
int &r; // ERROR: references must be initialized
int *p; // OK for pointer (though dangerous)
// RULE 2: can never be re-seated to another variable
int &r2 = a;
r2 = b; // careful! this does NOT re-point r2 -
// it COPIES b's value (20) into a!
cout << a; // 20
// RULE 3: no "null reference" exists
int *p2 = NULL; // pointer can be null
// reference: no such thing - always refers to something
Where it matters most: the swap function
#include <iostream>
using namespace std;
void swapPtr(int *x, int *y) { // pointer version
int t = *x; *x = *y; *y = t; // stars everywhere
}
void swapRef(int &x, int &y) { // reference version
int t = x; x = y; y = t; // clean, natural syntax
}
int main() {
int a = 5, b = 9;
swapPtr(&a, &b); // caller must pass addresses
cout << a << " " << b << endl;
swapRef(a, b); // caller passes variables directly
cout << a << " " << b << endl;
return 0;
}
Both work, but the reference version reads like normal code — no *, no & at the call site. This is exactly why C++ added references: clean pass-by-reference for functions.
Comparison table (6 differences)
| Point | Reference | Pointer |
|---|---|---|
| What it is | Second name (alias) | Variable storing an address |
| Initialization | Compulsory at declaration | Optional |
| Can be null | ❌ Never | ✅ Yes (nullptr) |
| Re-seat later | ❌ Never — bound for life | ✅ Can point anywhere anytime |
| Syntax to access | Direct: r = 5; | Dereference: *p = 5; |
| Arithmetic (p++) | ❌ No | ✅ Yes |
Which to use when
- Reference — function parameters and return values where "nothing" is impossible:
void update(Student &s). Cleaner and safer. - Pointer — when the target can change, can be absent (nullptr), or you need arrays/dynamic memory: linked lists, optional values,
new. - Interview line: "A reference is a permanently-bound, never-null alias; a pointer is a re-seatable, nullable address holder."
Nickname वाली analogy
int aman = 100;
int &monu = aman; // REFERENCE: SAME variable का दूसरा नाम
int *slip = &aman; // POINTER : address रखने वाला अलग variable
monu = 150; // nickname से change -> aman खुद बदल गया
cout << aman; // 150
3 rules जो references को अलग बनाते हैं
int a = 10, b = 20;
// RULE 1: जन्म पर ही initialize करना ज़रूरी
int &r; // ERROR: references must be initialized
int *p; // pointer के लिए OK (खतरनाक होते हुए भी)
// RULE 2: बाद में किसी और variable से नहीं जुड़ सकता
int &r2 = a;
r2 = b; // सावधान! यह r2 को re-point NAHI करता -
// b की value (20) को a में COPY करता है!
cout << a; // 20
// RULE 3: "null reference" जैसी चीज़ नहीं होती
int *p2 = NULL; // pointer null हो सकता है
// reference: ऐसा कुछ नहीं - हमेशा किसी से जुड़ा
सबसे ज़्यादा फर्क कहां: swap function
#include <iostream>
using namespace std;
void swapPtr(int *x, int *y) { // pointer version
int t = *x; *x = *y; *y = t; // हर जगह stars
}
void swapRef(int &x, int &y) { // reference version
int t = x; x = y; y = t; // साफ, natural syntax
}
int main() {
int a = 5, b = 9;
swapPtr(&a, &b); // caller को addresses भेजने पड़ते हैं
cout << a << " " << b << endl;
swapRef(a, b); // caller directly variables भेजता है
cout << a << " " << b << endl;
return 0;
}
दोनों काम करते हैं, लेकिन reference version normal code जैसा पढ़ा जाता है — call site पर न *, न &. C++ ने references इसीलिए जोड़े: functions के लिए साफ-सुथरा pass-by-reference.
Comparison table (6 अंतर)
| Point | Reference | Pointer |
|---|---|---|
| क्या है | दूसरा नाम (alias) | Address store करने वाला variable |
| Initialization | Declaration पर compulsory | Optional |
| Null हो सकता है | ❌ कभी नहीं | ✅ हां (nullptr) |
| बाद में re-seat | ❌ कभी नहीं — ज़िंदगी भर का बंधन | ✅ कभी भी कहीं भी point कर सकता है |
| Access syntax | Direct: r = 5; | Dereference: *p = 5; |
| Arithmetic (p++) | ❌ नहीं | ✅ हां |
कब कौन-सा use करें
- Reference — function parameters और return values जहां "कुछ नहीं" impossible है:
void update(Student &s). साफ और safe. - Pointer — जब target बदल सकता है, absent हो सकता है (nullptr), या arrays/dynamic memory चाहिए: linked lists, optional values,
new. - Interview line: "Reference permanently-bound, कभी-null-न-होने वाला alias है; pointer re-seatable, nullable address holder."
Frequently Asked Questions
What is the difference between a reference and a pointer?
A reference is a permanent alias for an existing variable — must be initialized, can never be null or re-seated — while a pointer is a separate variable holding an address that can be null, reassigned, and used in arithmetic.
Can a reference be null in C++?
No. A reference must be bound to a valid variable at declaration and stays bound for its entire life; nullability is a pointer feature.
What happens when we assign to a reference after initialization?
The assignment copies the value into the original variable — it does not re-point the reference, because references can never be re-seated.