Lesson 5 of 45 · C++
Lesson 13: Pointers Basics
Duration: 10 min
Lesson 5 of 45 · C++
Duration: 10 min
* suffix and can be assigned the address of a variable using the & (address‑of) operator.\n\n---\n\n## Example: Pointer declaration and dereferencing\ncpp\nint value = 42;\nint* ptr = &value; // ptr holds the address of value\nstd::cout << \"ptr points to \" << *ptr << \" (value)\\n\"; // dereference\n\n// Modify through pointer\n*ptr = 100;\nstd::cout << \"value is now \" << value << '\\n';\n\n\nPointers also support arithmetic for traversing arrays:\ncpp\nint arr[3] = {10, 20, 30};\nint* p = arr; // points to arr[0]\nstd::cout << *(p + 2) << '\\n'; // prints 30\n\n\n> Tip: Initialise pointers to nullptr when they do not point to a valid object. Dereferencing a null pointer yields undefined behaviour.\n\n---\n\n<Alert type="warning">Avoid raw pointers for ownership. Prefer smart pointers (unique_ptr, shared_ptr).\n