C Programming • C17 baseline, standards-specific features labeled
Pointer Arithmetic and Array Traversal
Move pointers only within an array object and use the one-past pointer solely as an end marker.
Adding one to a pointer advances by one element of its pointed-to type. Pointer arithmetic is defined within an array and to the one-past position; the one-past pointer may be compared but must not be dereferenced.
Example
#include <stdio.h>
int main(void) {
int values[] = {4, 8, 12};
int total = 0;
int *end = values + sizeof values / sizeof values[0];
for (int *current = values; current != end; ++current) {
total += *current;
}
printf("%d\n", total);
return 0;
}
What to expect
24
Key idea: Never form or dereference a pointer outside the same array object, except for the permitted one-past end marker.