C/C++ Arena

Pointer arithmetic walks an array

p starts at nums[0]. Each p++ moves the arrow to the next element, however many bytes that is. *p reads whichever box the arrow is on.

The loop stops when p reaches end, which points one past the last element. That address is fine to compare against but never to read.

#include <stdio.h>

int main(void) {
    int nums[4] = {3, 1, 4, 1};
    int sum = 0;
    int *end = nums + 4;
    for (int *p = nums; p != end; p++) {
        sum += *p;
    }
    printf("sum = %d\n", sum);
    return 0;
}

Output:

sum = 9

From the lesson: Pointers