C++20 sections · 1024 units
Open in Course

Dereferencing Pointers

Using * to get values

The * operator also means "value at address" when you use it with a pointer variable. If ptr holds the address of x, then *ptr gives you the value stored at that address. This operation is called dereferencing.

You can read and write through dereferenced pointers: *ptr = 10 changes the value at the address ptr points to. If ptr points to x, this changes x to 10. Two names, same memory location.

Here's a complete example: int x = 5; int* ptr = &x; cout << *ptr outputs 5. The *ptr follows the address back to the original variable and gets its value. You'll use this constantly.