Understanding References vs Pointers in C++

They look similar, but there are some differences between them. A reference is a variable that is another name for an existing variable, while a pointer is a variable that stores the memory address of another variable.

What Is a Reference?

A reference is a variable that is another name for an existing variable. A reference variable is created by storing the address of another variable.

A reference variable can be seen as a constant pointer with automatic indirection. Here, automatic indirection means that the compiler automatically applies the indirection operator (*).

Example of Reference:

int &a = i;

In the above declaration, ‘a’ is an alias for the variable ‘i’. We can also refer to the variable ‘i’ using the ‘a’ variable.

Let’s understand this through an example.

#include <iostream>
using namespace std;
int main() {
    int i = 8;    // Variable initialization
    int &a = i; // Create a reference variable
    cout << "The value of variable 'i' is: " << a;
    return 0;
}

In the above code, we created a reference variable ‘a’, which is an alias for the variable ‘i’. After creating the reference variable, we can access the value of ‘i’ through the ‘a’ variable.

What Is a Pointer?

A pointer is a variable that stores the memory address of another variable. The (*) operator can be used to dereference a pointer to access the memory location it points to.

👇 Click to Claim 👇
👉 C Language Knowledge Compilation

Differences Between References and Pointers

The differences between references and pointers are as follows:

  • Definition

A reference variable is another name for an existing variable. It is mainly used for ‘pass by reference’, where a reference variable is passed as a parameter to a function, and the function that receives this variable uses a copy of the original variable.

Let’s understand this through a simple example.

#include <iostream>
using namespace std;
void func(int&);
int main() {
    int a = 10;
    std::cout << "The value of variable 'a' is: " << a << std::endl;
    func(a);
    std::cout << "Now the value of variable 'a' is: " << a << std::endl;
    return 0;
}
void func(int &m) {
    m = 8;
}

Output:

The value of variable 'a' is: 10
Now the value of variable 'a' is: 8

A pointer is a variable that stores the memory address of another variable. It makes programming more convenient because it holds the memory address of a variable.

  • Declaration

We can declare a reference variable by adding the ‘&’ symbol before the variable. If this symbol is used in an expression, it will be treated as the address operator.

Before using a pointer variable, we should declare a pointer variable, which can be created by adding the ‘*’ operator before the variable.

  • Reassignment

A reference variable cannot be reassigned. Now, let’s look at a simple example:

#include <iostream>
using namespace std;
void func(int&);
int main() {
    int i;    // Variable declaration
    int k;    // Variable declaration
    int &a = i;
    int &a = k; // Error
    return 0;
}

The above code shows that declaring ‘int &a’ multiple times is not allowed. Therefore, the above program demonstrates that reference variables do not allow reassignment.

On the other hand, pointers can be reassigned. Reassignment is useful when using data structures like linked lists, trees, etc.

  • Memory Address

In the case of references, the reference and the actual variable refer to the same address. A new variable will not be assigned to the reference variable until the actual variable is deleted or goes out of scope.

Let’s understand this through an example.

#include <iostream>
using namespace std;
void func(int&);
int main() {
    int i;
    int &a = i;
    std::cout << "The memory address of variable 'a' is: " << &a << std::endl;
    std::cout << "The memory address of variable 'i' is: " << &i << std::endl;
    return 0;
}

Output:

The memory address of variable 'a' is: 0x7fff078e7e44
The memory address of variable 'i' is: 0x7fff078e7e4

In the case of pointers, the memory address of the pointer variable and the actual variable is different. Let’s understand this through an example.

#include <iostream>
using namespace std;
int main() {
    int k;
    int *p;
    p = &k;
    cout << "The memory address of variable 'p' is: " << &p;
    cout << "\nThe memory address of variable 'k' is: " << &k;
    return 0;
}

Output:

The memory address of variable 'p' is: 0x7ffcc5c164b8
The memory address of variable 'k' is: 0x7ffcc5c164b4
  • NULL Value

A NULL value cannot be assigned to a reference variable, but a NULL value can be assigned to a pointer variable.

  • Indirect Operation

A pointer can have a pointer to a pointer, providing multi-level indirection. For example:

#include <iostream>
using namespace std;
int main() {
    int *p;
    int a = 8;
    int **q;
    p = &a;
    q = &p;
    std::cout << "The value of variable 'q' is: " << *q << std::endl;
    return 0;
}

In the above code, the pointer ‘p’ points to the variable ‘a’, while ‘q’ is a double pointer that points to ‘p’. Therefore, we can say that the value of ‘p’ is the address of variable ‘a’, and the value of variable ‘q’ is the address of variable ‘p’.

Output:

The value of variable 'q' is: 0x7ffd104891dc

In the case of references, referencing to a reference is not allowed. If we try to do this, the C++ program will throw a compile-time error.

Let’s understand this through an example.

#include <iostream>
using namespace std;
int main() {
    int a = 8; // Variable initialization
    int &p = a; // Create a reference variable for 'a'
    int &&q = p;  // Referencing to reference is invalid, will throw an error
    return 0;
}

Output:

main.cpp: In function 'int main()':
main.cpp:18:10: error: cannot bind 'int' lvalue to 'int&&'
int &&q = p;
  • Arithmetic Operations

We know that arithmetic operations can be applied to pointers, called “pointer arithmetic”, but arithmetic operations cannot be applied to references. There is no “reference arithmetic” in C++.

Let’s look at a simple example of a pointer.

#include <iostream>
using namespace std;
int main() {
    int a[] = {1, 2, 3, 4, 5}; // Array initialization
    int *ptr;  // Pointer declaration
    ptr = a; // Assign base address to pointer ptr
    cout << "*ptr's value is: " << *ptr;
    ptr = ptr + 1;  // Increment ptr's value by 1
    std::cout << "\n*ptr's value is: " << *ptr << std::endl;
    return 0;
}

Output:

*ptr's value is: 1
*ptr's value is: 2

Let’s understand references through an example.

#include <iostream>
using namespace std;
int main() {
    int value = 90;  // Variable declaration
    int &a = value;   // Assign value to reference
    &a = &a + 5; // Reference variable cannot perform arithmetic operations, will throw an error
    return 0;
}

The above code will throw a compile-time error because reference variables are not allowed to perform arithmetic operations.

Understanding References vs Pointers in C++


 Popular Recommendations
  • CLion Tutorial – Advanced Configuration in CLion

  • C Language Algorithm – “Valid Number” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of C++ References

Leave a Comment