Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 10 / 21 · MEMORY AND POINTERS

Pointer arithmetic and array views.

Know when moving a pointer is defined and where the one-past boundary lies.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

Pointer arithmetic is scaled by the pointed-to type. If p points to an int array element, p + 1 points to the next int element, not to the next byte. That rule makes array traversal concise, but it is defined only within the same array object, including a special pointer one past its final element. Forming a pointer farther away or dereferencing the one-past pointer is outside the contract.

The expression a[i] is defined in terms of pointer arithmetic: it accesses *(a + i). This does not mean an array and a pointer are the same type. An array has an extent; in many expressions it converts to a pointer to its first element. That conversion loses the bound, so a function receiving the pointer needs the count separately. sizeof on a pointer gives pointer size, not the array's byte extent.

02 / THE RULES

What C guarantees.

Subtracting two pointers into the same array yields their distance in elements, represented by ptrdiff_t when the result fits. Comparing pointers with relational operators is meaningful within one array's ordered elements. Do not compare unrelated object addresses as though their numeric order had portable meaning. C's pointer rules are about relationships within objects, not arbitrary memory-map navigation.

A range can be represented by a beginning pointer and a one-past-end pointer, or by a beginning pointer and a count. Both forms require an invariant that the range belongs to one accessible array. The example uses the count form and advances a pointer only for known valid elements. The one-past pointer is never dereferenced.

03 / UNDER THE SURFACE

Why the detail matters.

The scaling in p + i comes from the pointed-to type. If sizeof(int) is four on one target, the machine address may advance by four bytes per element, but the C expression is written in elements and remains valid on targets where int has a different size. This is why casts to an integer address type are usually the wrong way to traverse an array. C's array relationship gives the compiler the type and extent assumptions it needs.

One-past-end is a deliberate boundary marker. It can be formed and compared or used as an end iterator in appropriate within-array operations, but it is not an extra element. A loop written p < end is valid only when p and end belong to the same array relationship. If p advances past end, even creating the result may leave defined behavior. The safest explanation is always in terms of an actual array and its known number of elements.

04 / CODE, EXPLAINED

See the rule in context.

pointer-arithmetic.c
#include <stdio.h>
#include <stddef.h>

int main(void) {
    int data[] = {3, 5, 8, 13};
    size_t count = sizeof data / sizeof data[0];
    const int *p = data;
    int total = 0;
    for (size_t i = 0; i < count; ++i) total += *(p + i);
    printf("Total: %d\n", total);
    return 0;
}
FOLLOW THE EXECUTION

data has four elements. p begins at the first one, and each p + i identifies an element while i is below count. The loop adds 3, 5, 8, and 13 to reach 29. On the final condition check, i equals 4 and the loop stops before *(p + 4) could be evaluated. That expression would try to read from the one-past position, which is not an element.

05 / THE BOUNDARY

Where the rule stops.

Pointer arithmetic cannot be used as a general substitute for an integer address calculation. Its defined range is tied to an actual array object and its one-past position.

06 / THE CONNECTION

Keep this model.

The same rules apply when storage comes from malloc, but then the program must also track who will release that storage and when. Dynamic allocation adds an ownership obligation that an automatic array does not have.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.