C++ Pointers
Every variable lives somewhere in memory, and every location in memory has an address. A pointer is a variable that stores such an address. Pointers are one of the features that make C++ powerful — and one of the features that demand the most care.
Address-of and dereference
Two operators do all the work:
| Operator | Name | Meaning |
|---|---|---|
&x |
address-of | "where does x live?" |
*p |
dereference | "the value stored at the address in p" |
A pointer's type says what it points to: int* points to an int, string* to a string.
int age = 30;
int* p = &age; // p holds the address of age
cout << &age; // prints something like 0x7ffd5c3a1b2c
cout << p; // the same address
cout << *p; // 30Actual addresses differ on every run, so the runnable examples below compare pointers instead of printing them:
#include <iostream>
using namespace std;
int main() {
int age = 30;
int* p = &age;
cout << boolalpha << (p == &age) << "\n"; // p holds age's address
cout << *p << "\n"; // read through the pointer
*p = 31; // write through the pointer
cout << age << "\n";
}true
30
31
Changing *p changes age, because both refer to the same memory.
Note: The
*symbol has two jobs. In a declaration (int* p) it means "p is a pointer". In an expression (*p = 31) it means "dereference p".
Null pointers
A pointer that points nowhere should hold nullptr. Dereferencing a null pointer is undefined behaviour (usually a crash), so check first:
#include <iostream>
using namespace std;
void printValue(const int* p) {
if (p == nullptr) {
cout << "(no value)\n";
return;
}
cout << *p << "\n";
}
int main() {
int x = 7;
printValue(&x);
printValue(nullptr);
}7
(no value)
Unlike references, pointers can be null and can be re-pointed to other objects. That makes them useful for "optional" parameters and for data structures such as linked lists and trees.
Pointers and arrays
The name of an array converts to a pointer to its first element. Adding 1 to a pointer moves it to the next element (not the next byte) — this is called pointer arithmetic:
#include <iostream>
using namespace std;
int main() {
int data[] = {10, 20, 30, 40};
int* p = data; // same as &data[0]
cout << *p << "\n";
cout << *(p + 2) << "\n"; // same as data[2]
p++;
cout << *p << "\n";
// walk the array with a pointer
for (int* q = data; q != data + 4; q++) {
cout << *q << " ";
}
cout << "\n";
}10
30
20
10 20 30 40
In fact data[i] is defined as *(data + i). This is also why an array passed to a function "forgets" its size — the function only receives a pointer.
Pointers to structs: the arrow operator
To access a member through a pointer, (*p).member works but is clumsy. The arrow p->member means exactly the same:
#include <iostream>
#include <string>
using namespace std;
struct Player {
string name;
int hp;
};
void heal(Player* p, int amount) {
p->hp += amount;
}
int main() {
Player hero{"Kai", 40};
Player* ptr = &hero;
heal(ptr, 25);
cout << ptr->name << " has " << ptr->hp << " HP\n";
cout << (*ptr).name << " (same thing)\n";
}Kai has 65 HP
Kai (same thing)
const and pointers
const can apply to the data, the pointer, or both. Read the declaration from right to left:
| Declaration | Can change *p? |
Can change p? |
|---|---|---|
int* p |
yes | yes |
const int* p |
no | yes |
int* const p |
yes | no |
const int* const p |
no | no |
Pointers vs references
| Pointer | Reference | |
|---|---|---|
| Can be null | yes (nullptr) |
no |
| Can be re-pointed | yes | no |
| Syntax | *p, p->m |
used like the variable |
| Must be initialised | no (but should be) | yes |
Tip: Prefer references when something must always exist, and pointers only when "nothing" is a valid option or you need to re-point. For owning dynamic memory, prefer smart pointers (covered later).
Test yourself
Given int x = 5; int* p = &x;, what is *p?
- The address of x
- 5
- The address of p
- 0
What does p->name mean when p is a pointer to a struct?
- p.name
- (*p).name
- *p.name
- &p.name
If p points to arr[1] of an int array, where does p + 1 point?
- One byte further
- arr[2]
- arr[0]
- It is invalid