Pointer dereference in C++

Level 378 keystrokes

ptr holds the address of x. *ptr reads x through the pointer (10), and assigning to *ptr changes x itself, so x then prints 20.

C++ code

1int x = 10;
// Stores the address of x in ptr
2int* ptr = &x;
// Dereferencing with * reads the value at that address: 10
3cout << *ptr << endl;
// Assigning through *ptr writes into x
4*ptr = 20;
// x itself has changed: prints 20
5cout << x << endl;

Output

10
20
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 8
  • ;× 5
  • *× 3
  • =× 3
  • &× 1

More C++ examples at level 3

← C++ typing practice