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

C++ References

A reference is a second name for the same variable, like a nickname for a person. Changing it through one name changes it for both.
Syntax
cpp
data_type &reference_name = variable_name;  // an alias for variable_name

What is a Reference?

A reference in C++ is an alias for an existing variable, sharing the exact same memory address as the original, so once created, any change made through the reference directly updates the variable it refers to.

Unlike a pointer, a reference cannot be reseated to refer to something else later.

Example: What is a Reference?

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 &ref = value;
	ref = 10;
	// Print to the console with cout
	std::cout << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Variables via References

Because a reference shares its target's memory location, modifying the reference is indistinguishable from modifying the original variable — there's no separate copy to keep in sync.

This makes references useful whenever you want an alternate name for the same piece of data.

Example: Modifying Variables via References

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 50
	int score = 50;
	int &scoreRef = score;
	scoreRef += 20;
	// Print to the console with cout
	std::cout << score << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pass by Reference

You can pass arguments to functions by reference, which avoids copying large variables or objects and lets the function update the caller's original arguments directly.

This is typically faster than passing by value for anything larger than a primitive type.

Example: Pass by Reference

cpp
// Include std::cout and std::cin
#include <iostream>

void increase(int &x) {
	x += 1;
}

// Program execution starts in main()
int main() {
	// Declare num and set it to 5
	int num = 5;
	increase(num);
	// Print to the console with cout
	std::cout << num << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Constant References

If you want the performance benefit of reference passing without letting the function modify your data, pass a const reference. It avoids the cost of copying large objects while still guaranteeing the caller's original value stays untouched.

Example: Constant References

cpp
#include <iostream>
#include <string>

void printName(const std::string &name) {
	std::cout << name << std::endl;
	// name = "Changed"; // compile error
}

int main() {
	printName("Alex");
	return 0;
}

Returning References from Functions

Functions can return references, which is useful for chaining method calls or exposing a modifiable slot inside a container.

Be careful never to return a reference to a local variable, since it will dangle and point to invalid memory the instant the function returns.

Example: Returning References from Functions

cpp
// Include std::cout and std::cin
#include <iostream>

int scores[3] = {10, 20, 30};

int &getScore(int index) {
	// Send scores[index] back to the caller
	return scores[index];
}

// Program execution starts in main()
int main() {
	getScore(1) = 99;
	// Print to the console with cout
	std::cout << scores[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	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. #}
Common Mistakes
  1. Declaring a reference without initializing it, such as int& r;, which is a compile error because a reference must bind at declaration.
  2. Expecting r = y; to make the reference refer to y, when it copies the value of y into the original variable.
  3. Returning a reference to a local variable, which no longer exists after the function ends.

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.