Pointer Arithmetic in C

Level 4124 keystrokes

Demonstrates pointer arithmetic to traverse array elements.

C code

// Array of 4 integers
1int arr[] = {10, 20, 30, 40};
// Pointer points to first element
2int *ptr = arr;
// Dereference to get value at ptr (10)
3printf("%d\n", *ptr);
// Increment pointer to next element
4ptr++;
// Now points to second element (20)
5printf("%d\n", *ptr);
// Access two positions ahead (40)
6printf("%d\n", *(ptr + 2));

Output

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

  • ,× 6
  • ;× 6
  • "× 6
  • (× 4
  • )× 4
  • *× 4
  • \× 3
  • %× 3
  • +× 3
  • =× 2

More C examples at level 4

← C typing practice