Systems Programming · 9 min read · Intermediate
Pointer Arithmetic & Const Correctness
Traverse contiguous arrays using pointer increments and understand const int *p vs. int *const p.
What you'll learn
- Understand why p + 1 advances by sizeof(*p) bytes, not 1 byte
- Traverse arrays cleanly with pointer start/end bounds
- Read const pointer declarations accurately right-to-left
In C, adding 1 to an int *p advances the address by sizeof(int) bytes (typically 4 bytes), pointing directly to the next integer in the array. In fact, arr[i] is literally defined by the C standard as *(arr + i).
Fill-in-the-Blank Exercise
Sum Array with Pointer Walk
Dereference p to add each element, then advance p to the next int.
int sum = 0;
for (const int *p = arr; p < arr + n; ++) {
sum += p;
}Live Code Lab
Cpointer_math.c
Expected OutputClick “Run Live” to compile with GCC 13 (C11)
Sum via pointer walk: 100
Check Your Understanding
Score: 0 / 1
1. If int *p points to address 1000 on a system where sizeof(int) is 4, what address does p + 2 represent?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.