bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/C++/C++ Tutorial
C++•C++ Tutorial

C++ Dereference

Flash cards

Review the key moves

1/4
Core idea

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.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ food = "Pizza"; // Variable declaration
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Note that the * sign can be confusing here, as it does two different things in our code:
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).
Get Memory Address and Value

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 .

Previous

C++ Pointers

Next

C++ Modify Pointers