References in C++
In this lesson: What references are in C++, pass by reference, const references and reference vs pointer — with output. In Hindi & English.
What is a reference?
A reference is simply another name — an alias — for a variable that already exists. Whatever you do to the reference happens to the original, because they are the same thing under two names.
Creating a reference
Use & in the declaration and bind it to an existing variable straight away:
#include <iostream>
using namespace std;
int main() {
int x = 10;
int &r = x; // r is an alias for x
r = 20; // changes x
cout << x << " " << r << "\n";
return 0;
}20 20
A reference must be initialised when declared, and it can never be rebound to a different variable afterwards.
References as parameters
The most common use: let a function change the caller's variable, without pointers:
void swap(int &a, int &b) {
int t = a; a = b; b = t;
}
int main() {
int x = 1, y = 2;
swap(x, y);
cout << x << " " << y; // 2 1
}2 1
const references
A const reference lets a function read a big value efficiently (no copy) while promising not to change it:
void print(const string &s) { // no copy, cannot modify s
cout << s << "\n";
}This is the standard, efficient way to pass strings, vectors and other large objects you only need to read.
Returning a reference
Functions can return a reference, but never return a reference to a local variable — it is destroyed when the function ends. Returning a reference to something that outlives the call (like an array element) is fine.
References vs pointers
| Reference | Pointer | |
|---|---|---|
| Can be null? | No | Yes (nullptr) |
| Must be initialised? | Yes | No |
| Can be reseated? | No | Yes |
| Access value | Use like the variable | Needs * |
See the pointers lesson for when a pointer is the right choice.
Common mistakes
- Forgetting to initialise a reference — it must bind at declaration.
- Expecting to reseat a reference to a new variable later (not possible).
- Returning a reference to a local variable (dangling).
- Copying a big object by value when a
constreference would be faster.
Write a function that takes an int& and doubles it. Then write a print(const vector<int>&) that prints a list without copying it. Notice how the caller's data is affected in each case.
Summary
- A reference is an alias for an existing variable.
- It must be initialised and can never be reseated or null.
- Reference parameters let functions change the caller's data.
constreferences pass large values efficiently and safely.- References are usually cleaner than pointers for everyday use.
Frequently Asked Questions
What is a reference in C++?
int &r = x; binds r to x, using r is exactly like using x. References must be initialised when declared and cannot later refer to a different variable.What is the difference between a reference and a pointer in C++?
* operator to reach its value. References are usually safer and cleaner.Why use references as function parameters?
void f(int &x) means changes to x inside f are seen by the caller, unlike pass-by-value which copies.What is a const reference in C++?
const T&, lets a function read a large object without copying it while promising not to change it. It is the standard way to pass big values like strings or vectors efficiently and safely.Can a reference be null in C++?
nullptr instead.