Mastering C++
Simple Pointers
Mastering Pointers1. Pointers to Stack Memory
A pointer stores memory addresses. If you create a regular variable like int x = 42;, it lives on the stack. To get its memory address, you use &x. That’s called the address-of operator.
You can store this address in a pointer like int* ptr = &x;. The * tells us that ptr is a pointer to an int.
To get the actual value stored at the address in ptr, you write *ptr. This is called dereferencing the pointer. So if ptr points to x, then *ptr is the same as x. You can also use *ptr to change the value of x directly by doing something like *ptr = 99;.
Members only
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 21 more lessons in Mastering C++.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.