Pointers and arrays in C++

Level 3110 keystrokes

An array name converts to a pointer to its first element. p + i points i elements further on, so *(p + i) is the same as arr[i]. Prints 10 20 30.

C++ code

1int arr[] = {10, 20, 30};
// arr converts to a pointer to arr[0]; no & is needed
2int* p = arr;
3for (int i = 0; i < 3; i++) {
// p + i moves by i elements (not bytes); * reads the value there
4 cout << *(p + i) << " ";
5}
// Ends the line after 10 20 30
6cout << endl;

Output

10 20 30 
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.

  • <× 7
  • ;× 6
  • +× 3
  • =× 3
  • ,× 2
  • "× 2
  • (× 2
  • )× 2
  • {× 2
  • }× 2

More C++ examples at level 3

← C++ typing practice