Pointer Basics
Understanding pointers and addresses
Interview Relevant: Critical C++ concept
Pointer Basics
Pointers store memory addresses of other variables.
Code Examples
Basic pointer operations and const variations.
cpp
1int x = 10;
2int* ptr = &x; // ptr holds address of x
3
4// Dereference to get value
5cout << *ptr; // 10
6
7// Modify through pointer
8*ptr = 20; // x is now 20
9
10// Null pointer
11int* nullPtr = nullptr; // C++11
12if (ptr != nullptr) { }
13
14// Pointer to pointer
15int** pptr = &ptr;
16cout << **pptr; // 20
17
18// const with pointers
19const int* ptr1 = &x; // Can't modify *ptr1
20int* const ptr2 = &x; // Can't modify ptr2
21const int* const ptr3 = &x; // Can't modify either