Skip to main content
C++ Fundamentals for Beginners to Advanced
CHAPTER 11 Beginner

Pointers and References in C++

Updated: May 17, 2026
5 min read

# CHAPTER 11

Pointers and References

1. Introduction

Welcome to the defining feature of C++: Direct Memory Access. Languages like Python and Java hide memory management from you. C++ gives you the keys to the castle. Understanding Pointers and References is the single most important hurdle to becoming a C++ expert.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the Address-of operator (&).
  • Declare and use Pointers.
  • Dereference pointers (*) to access data.
  • Understand Null pointers.
  • Use References as aliases.
  • Implement Pass-by-Reference in functions.

3. The Address-of Operator (&)

Every variable you create is stored at a specific memory address in RAM. You can find out *where* a variable lives using the & operator.
cpp
123456789
#include <iostream>
using namespace std;

int main() {
    int score = 100;
    cout << "Value: " << score << endl;
    cout << "Address: " << &score << endl; // Prints something like 0x7ffee123
    return 0;
}

4. What is a Pointer?

A Pointer is simply a variable that stores a *memory address* instead of a standard value.

Syntax: dataType* pointerName;

cpp
1234
int score = 100;
int* ptr = &score; // ptr now holds the memory address of score

cout << ptr << endl; // Prints the address (e.g., 0x7ffee123)

Memory Visualization:

text
1234567891011
score variable:
+----------+
|   100    |
+----------+
 Address: 0x1000

ptr variable:
+----------+
|  0x1000  |  <--- Points to score!
+----------+
 Address: 0x2000

5. Dereferencing a Pointer (*)

If a pointer holds an address, how do we get the actual data at that address? We use the Dereference operator (*).
cpp
12345678
int score = 100;
int* ptr = &score;

cout << *ptr << endl; // Prints 100!

// You can also change the value THROUGH the pointer
*ptr = 200;
cout << score << endl; // Score is now 200!

6. Null Pointers (nullptr)

An uninitialized pointer points to a random memory address (garbage). Dereferencing it will crash your program! If you declare a pointer but don't have an address for it yet, ALWAYS assign it to nullptr (Modern C++ replacement for NULL).
cpp
1
int* p = nullptr; // Safe. 

7. References

A Reference is an alias (an alternative name) for an existing variable. Once a reference is created, it cannot be changed to refer to another variable.

Syntax: dataType& refName = variable;

cpp
12345
int apples = 5;
int& fruit = apples; // fruit is now an alias for apples

fruit = 10;
cout << apples; // Prints 10!

*Note: A reference is basically a pointer in disguise that is safer and easier to use because it doesn't require dereferencing (*).*

8. Mini Project: Swap Values

By default, C++ functions use Pass-by-Value (they make copies of variables). If a function swaps copies, the originals in main() don't change. We fix this by passing References or Pointers.
cpp
12345678910111213141516171819
#include <iostream>
using namespace std;

// Pass-by-Reference
void swapNumbers(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10, y = 20;
    cout << "Before: x=" << x << " y=" << y << endl;
    
    swapNumbers(x, y); // Passes the actual variables, not copies
    
    cout << "After: x=" << x << " y=" << y << endl;
    return 0;
}

9. Common Mistakes

  • Confusing * in declaration vs dereferencing: int* p means "declare a pointer". *p = 5 means "go to the address in p and set it to 5".
  • Dereferencing nullptr or Garbage: int* p; *p = 5; -> Segmentation Fault Crash!
  • Returning References to Local Variables:
cpp
12345
// DISASTROUS BUG!
int& badFunction() {
    int local = 5;
    return local; // local is destroyed when function ends, returning a dangling reference!
}

10. Exercises

  1. 1. Declare a double variable. Create a pointer to it. Change the value of the double using the pointer, then print it.
  1. 2. Rewrite the swapNumbers function using Pointers (int*) instead of References.

11. MCQ Quiz with Answers

Question 1

What does the & operator do when placed before a variable?

Question 2

What is a pointer?

Question 3

Which operator is used to dereference a pointer?

Question 4

What is the Modern C++ keyword for a null pointer?

Q5. Can a reference be reassigned to alias a different variable after initialization? a) Yes b) No Answer: b) No (Unlike pointers, references are locked to their original variable).
Question 6

What happens if you dereference a nullptr?

Question 7

In void update(int& x), how is x passed?

Question 8

Why use Pass-by-Reference for large objects (like a massive 3D model)?

Question 9

Which of the following declares a pointer to a char?

Question 10

What is a Dangling Pointer?

12. Interview Questions

  • Q: Explain the difference between a Pointer and a Reference.
  • Q: What is Pass-by-Value vs. Pass-by-Reference?
  • Q: What is a Segmentation Fault and what usually causes it?

13. Summary

Pointers store memory addresses, allowing direct memory manipulation via the dereference (*) operator. References are safer, cleaner aliases for existing variables. Both are crucial for avoiding unnecessary data copying in functions (Pass-by-Reference) and are the foundation of advanced data structures.

14. Next Chapter Recommendation

In Chapter 12: Dynamic Memory Allocation, you will learn how to manually request chunk of memory from the OS while your program is running using new and delete.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·