Pointer Basics in C

Level 385 keystrokes

Demonstrates pointer declaration, the address-of operator (&) and the dereference operator (*). The printed address differs between runs and machines.

C code

// An ordinary int stored somewhere in memory
1int x = 10;
// int * means "pointer to int"; &x gives the address of x
2int *ptr = &x;
// *ptr reads the value stored at that address: 10
3printf("Value: %d\n", *ptr);
// %p prints the address itself in hex; it changes on every run
4printf("Address: %p\n", ptr);

Output

Value: 10
Address: 0x7ffd5e8c3a4c
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.

  • ;× 4
  • "× 4
  • ,× 2
  • :× 2
  • (× 2
  • )× 2
  • *× 2
  • \× 2
  • %× 2
  • =× 2

More C examples at level 3

← C typing practice