Learn C Programming

Lesson 5 of 10 · Pointers and Memory

Module 11 · Pointers and Memory

Pointer Arithmetic, and const in Three Places

Free with an accountReading

Kenji has heard that pointers are faster than indexes, so he rewrites Module 9's sum of five marks with one. The Playground prints 350, the right total. Compiler Explorer prints 33116 for the same program. Neither compiler said a word, and the loop is wrong on both. This lesson is about the arithmetic that moves a pointer, and the rule Kenji broke.

Checking what this lesson needs for you…

What is inside Pointer Arithmetic, and const in Three Places

  1. Kenji's sum that is right on one machine
  2. p + 1 moves one element, not one byte
  3. Subtracting two pointers counts the elements between them
  4. One past the end: form it, never read it
  5. const in three places: which box is locked
  6. A read-only walk with const int *
  7. Casting const away breaks the promise