Const Pointer in C

Level 4176 keystrokes

Demonstrates const pointer vs pointer to const data.

C code

// Two integer variables
1int a = 10, b = 20;
// Pointer to const int: the pointer can change, *ptr1 = ... is an error
2const int *ptr1 = &a;
// Const pointer to int: the value can change, ptr2 = ... is an error
3int *const ptr2 = &a;
// Print the value through ptr1 (10)
4printf("*ptr1: %d\n", *ptr1);
// ptr1 can point to different address
5ptr1 = &b;
// Now ptr1 points to b
6printf("*ptr1: %d\n", *ptr1);
// ptr2 can modify the value it points to
7*ptr2 = 30;
// a is now 30
8printf("*ptr2: %d\n", *ptr2);

Output

*ptr1: 10
*ptr1: 20
*ptr2: 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.

  • *× 9
  • ;× 8
  • "× 6
  • =× 6
  • ,× 4
  • :× 3
  • (× 3
  • )× 3
  • \× 3
  • &× 3

More C examples at level 4

← C typing practice