C++ · Pointers
What a pointer is, & and * operators, nullptr, pointer arithmetic over an array, and why dangling pointers are dangerous.
This is the topic Python hid from me. A pointer is a variable whose value is a memory address.
&xgives the address ofx.int* p = &x;declares a pointer to an int.*pdereferences the pointer: it reads or writes the value at that address.nullptrmeans “points to nothing”. Dereferencing it is undefined behaviour.- Pointer arithmetic:
p + 1moves forward by one element, not one byte.
Python names behave like references to objects. C++ lets me work with the addresses directly, which is powerful but easy to get wrong.
#include <iostream>
void bump(int* p) { if (p) *p += 10; } // modify caller's variable
int main() {
int x = 5;
int* p = &x;
std::cout << "x=" << x << " *p=" << *p << " (p holds an address: " << (p != nullptr) << ")\n";
*p = 42;
std::cout << "after *p = 42, x=" << x << '\n';
bump(&x);
std::cout << "after bump, x=" << x << '\n';
int arr[4] = {10, 20, 30, 40};
int* q = arr; // array decays to pointer to first element
for (int i = 0; i < 4; ++i) std::cout << *(q + i) << ' ';
std::cout << "\ndistance arr[3]-arr[0]: " << (&arr[3] - &arr[0]) << " elements\n";
int* none = nullptr;
bump(none); // safe: checked inside
std::cout << "null handled\n";
}
Output:
x=5 *p=5 (p holds an address: 1)
after *p = 42, x=42
after bump, x=52
10 20 30 40
distance arr[3]-arr[0]: 3 elements
null handledSources
- Pointer declaration — cppreference.com
- Introduction to pointers — LearnCpp.com
- nullptr — cppreference.com