Introduction to C++ | References vs Pointers

References vs Pointers in C++

In C++, both references and pointers allow you to refer to the memory location of a variable, but they behave in different ways. Understanding the differences between references and pointers is crucial for writing efficient and maintainable C++ code. Let’s dive into how they differ and when to use each one.

What is a Reference?

A reference in C++ is an alias for an existing variable. Once a reference is initialized to a variable, it cannot be made to refer to another variable. References are generally used to simplify passing arguments to functions and avoid unnecessary copies of data.

Syntax:

Syntax of Reference


        type& referenceName = variableName;
                    

What is a Pointer?

A pointer is a variable that stores the memory address of another variable. Unlike references, pointers can be reassigned to point to different variables. They can also be null, meaning they do not point to any valid memory location.

Syntax:

Syntax of Pointer


        type* pointerName = &variableName;
                    

Key Differences Between References and Pointers

Let’s compare the key differences between references and pointers:

Examples: References vs Pointers

1. Reference Example:

Code Example: Reference


        #include <iostream>
        using namespace std;
        
        int main() {
            int a = 10;
            
            // Declare a reference to the variable 'a'
            int& ref = a;
            
            // Modify the value of 'a' through the reference
            ref = 20;
            
            cout << "a: " << a << endl;  // a is now 20
            cout << "ref: " << ref << endl;  // ref is also 20
            
            return 0;
        }
                    
Output:
a: 20
ref: 20
2. Pointer Example:

Code Example: Pointer


        #include <iostream>
        using namespace std;
        
        int main() {
            int a = 10;
            
            // Declare a pointer to the variable 'a'
            int* ptr = &a;
            
            // Modify the value of 'a' through the pointer
            *ptr = 20;
            
            cout << "a: " << a << endl;  // a is now 20
            cout << "ptr: " << *ptr << endl;  // ptr is also 20
            
            return 0;
        }
                    
Output:
a: 20
ptr: 20

When to Use References vs Pointers

Here are some guidelines for when to use references and pointers in C++:

Summary of Key Differences

Aspect Reference Pointer
Initialization Must be initialized when declared Can be initialized or assigned later
Reassignment Cannot be reassigned to refer to a different variable Can point to different variables during its lifetime
Nullability Cannot be null Can be null
Dereferencing No need to dereference Dereferencing required with *
Memory Management No memory management Can be used for dynamic memory allocation

Pro Tip:

💡 Pro Tip

Use references when you are certain that the variable you are referring to will always exist and you don’t need to change the reference. Use pointers when you need the flexibility to change the variable being referred to or when dealing with dynamic memory allocation.