Pointers and References in C++
In C++, every variable occupies a memory location identified by an address.
Summary
In C++, every variable occupies a memory location identified by an address. Pointers and references are mechanisms to access data indirectly through these addresses, a concept known as indirection. A pointer is a variable holding the address of another variable and is declared with an asterisk (e.g., int* p). Dereferencing a pointer using *p accesses the value stored at that address. Pointers can be null (nullptr), meaning they point to no object, and can be reassigned to different objects. In contrast, a reference is an alias for an existing object, declared with an ampersand (e.g., int& r), must be initialized upon declaration, cannot be null, and cannot be re-bound to refer to another object. Both pointers and references enable functions to modify caller variables and help avoid costly data copying. Pointers are also central to building dynamic data structures by linking elements via addresses. However, pointers must be used carefully: dereferencing null or dangling pointers (pointing to objects that no longer exist) causes undefined behavior. Modern C++ encourages using values and references where possible, reserving pointers for cases requiring optional or changeable references, while smart pointers manage dynamic memory more safely. Understanding these concepts is fundamental to grasping C++ memory and computational models.
| Property | Pointer | Reference |
|---|---|---|
| Can be null | Yes () |
🧠 Key Concepts
- Pointer declaration
- Dereferencing
- Null pointer
- Reference aliasing
- Indirection
- Pointer reassignment
- Lifetime of objects
- Undefined behavior
- Modern C++ best practices
🧠 Quick Check
See what you remember from the summary.
What does the expression `*p` mean when `p` is a pointer in C++?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Pointers and References in C++
OVERVIEW
Every variable in a running program lives somewhere in memory, and that location has an address. Pointers and references are the two C++ mechanisms that let code work with where a value is stored instead of only with the value itself. Using them is called indirection: reaching a value through something that refers to it.
This note introduces pointers and references as part of the computational and memory model, in standard C++17. It does not cover allocating memory dynamically or pointer arithmetic; those build on this foundation. Most everyday C++ is written with ordinary values and references, and raw pointers are one tool among several, used for specific purposes explained below.
VARIABLES AND ADDRESSES
When you write `int x = 10;`, the program reserves some memory to hold an `int` and associates the name `x` with it. That memory has an address. The address-of operator `&` gives the address of a variable:
int x = 10; // &x is the address of x
Addresses are numbers determined when the program runs. You almost never care about the specific number; you care about the relationship: "this thing refers to that variable."
POINTERS
A pointer is a variable whose value is an address. Its type records what kind of value it points to.
#include <iostream>
int main() { int x = 10; int* p = &x; // p holds the address of x: "p points to x"
std::cout << x << '\n'; // 10 std::cout << *p << '\n'; // 10: the value that p points to
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.