Lesson 13 of 45
C++Lesson 13: Pointers Basics
10 min
Course progress29%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 13 of 45
C++10 min
Keep these ideas in mind as you work through the lesson.
Pointers store the memory address of another object. A pointer is declared with the * suffix and can be assigned the address of a variable using the & (address‑of) operator.
int value = 42;
int* ptr = &value; // ptr holds the address of value
std::cout << \"ptr points to \" << *ptr << \" (value)\
\"; // dereference
// Modify through pointer
*ptr = 100;
std::cout << \"value is now \" << value << '\
';
Pointers also support arithmetic for traversing arrays:
int arr[3] = {10, 20, 30};
int* p = arr; // points to arr[0]
std::cout << *(p + 2) << '\
'; // prints 30
Tip: Initialise pointers to
nullptrwhen they do not point to a valid object. Dereferencing a null pointer yields undefined behaviour.
<Alert type="warning">Avoid raw pointers for ownership. Prefer smart pointers (unique_ptr, shared_ptr).