Pointer to Pointer in C

Level 5279 keystrokes

Creates a pointer to an int and a pointer to that pointer, reads 100 through one and two levels of indirection, then changes the value to 200 through **pptr. It also prints the address held in ptr twice, once via *pptr; the address differs from run to run.

C code

// The int that both pointers lead to
1int value = 100;
// ptr holds the address of value
2int *ptr = &value;
// pptr holds the address of ptr: a pointer to a pointer
3int **pptr = &ptr;
4
// Reads the variable directly: prints Value: 100
5printf("Value: %d\n", value);
// One dereference follows ptr to value: prints *ptr: 100
6printf("*ptr: %d\n", *ptr);
// Two dereferences: pptr → ptr → value, prints **pptr: 100
7printf("**pptr: %d\n", **pptr);
8
// Writes through both levels, so value itself becomes 200
9**pptr = 200;
// Prints After change: 200
10printf("After change: %d\n", value);
// Prints the address stored in ptr; %p expects void *, and the value differs from run to run
11printf("ptr addr: %p\n", (void *)ptr);
// *pptr is ptr itself, so the same address is printed
12printf("*pptr addr: %p\n", (void *)*pptr);

Output

Value: 100
*ptr: 100
**pptr: 100
After change: 200
ptr addr: 0x7fff5fbff8ac
*pptr addr: 0x7fff5fbff8ac
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.

  • *× 15
  • "× 12
  • ;× 10
  • (× 8
  • )× 8
  • ,× 6
  • :× 6
  • \× 6
  • %× 6
  • =× 4

More C examples at level 5

← C typing practice