Flash cards
Review the key moves
What is the main idea behind C++ Dereference?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
___ food = "Pizza"; // Variable declarationPut the learning moves in the order that makes the concept easiest to apply.
Get Memory Address and Value
In the example from the earlier example, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). However, you can also use the pointer to get the value of the variable, by using the * operator (the dereference operator):
Example
string food = "Pizza"; // Variable declaration
string* ptr = &food;
// Pointer declaration // Reference: Output the memory address of food with the pointer (0x6dfed4) cout << ptr << "\n"; // Dereference: Output the value of food with the pointer (Pizza) cout << *ptr << "\n";Note that the * sign can be confusing here, as it does two different things in our code:
- When used in declaration (string* ptr), it creates a pointer variable .
- When not used in declaration, it act as a dereference operator .