Module 6 · Pointers and Memory — topic 2 of 5
Pointer Arithmetic and Arrays
Adding to a pointer moves it by whole elements, not bytes. Learn why a[i] means *(a + i), how to walk arrays with pointers, pointer subtraction and the limits of legal pointer arithmetic.
Learning objectives
- Explain that p + 1 advances by sizeof(*p) bytes
- Use the equivalence a[i] == *(a + i)
- Traverse arrays and strings with pointers
Definition
Pointer arithmetic adds or subtracts integers to pointers into an array, moving by multiples of the pointed-to type's size; subtracting two pointers into the same array yields the number of elements between them as a ptrdiff_t.Introduction
If p points to an int at address 1000, p + 1 is address 1004 on a system with 4-byte ints, not 1001. The compiler scales the arithmetic by the element size so that p + 1 always means “the next element”.
This is exactly how array indexing works. In C, a[i] is defined as *(a + i): start at the array, move i elements, dereference.
Why it matters
Pointer arithmetic is how library functions like strlen and memcpy are written, and how you efficiently walk buffers.
Understanding it removes the mystery from arrays decaying to pointers and from the many “what does this print” interview questions about *p++.
Detailed explanation
In most expressions an array name decays to a pointer to its first element: int *p = arr; means p = &arr[0]. Exceptions: sizeof arr, &arr and string-literal initialisers.
p + n points n elements ahead; p - n points back. p++ moves to the next element. q - p gives the number of elements between two pointers into the same array.
Valid pointer values are addresses of array elements and the address one past the last element (which may be compared but not dereferenced). Moving outside that range is undefined behaviour even if you never dereference it.
*p++ parses as *(p++): use the current element, then advance the pointer. (*p)++ increments the element instead.
Syntax
- Arithmetic is in elements, not bytes.
- Only compare/subtract pointers into the same array.
endmay be compared withpbut must not be dereferenced.
Code examples
Indexing is pointer arithmetic
30 30 30 30 step = 4 bytes
Because a[i] is *(a + i) and addition commutes, 2[arr] is valid C — a curiosity, not a style.
Walking an array with a pointer
sum = 14
More examples
String functions written with pointers
pointer magic has 13 chars
a=5 b=6 v={5,7,7}Step-by-step explanation
while (*s) s++;advances until the character is'\0'(value 0, false).s - startis the number of characters passed: the length.- In
my_strcpy,*dst++ = *src++copies one character and advances both pointers; the loop ends after copying the terminator. *p++reads v[0] (5) then movespto v[1].(*p)++returns v[1]'s old value (6) and increments v[1] to 7.
Expected output
Running “String functions written with pointers” prints exactly:
Practical applications
memcpy,memsetandstrlenin the C library use pointer loops.- Network parsers advance a pointer through a packet buffer field by field.
- Image processing iterates over pixel buffers with pointers for speed.
Best practices
- Prefer indexing for clarity; use pointers when they make the code simpler or faster.
- Use the half-open range
[begin, end)for pointer loops. - Use
constpointers for data you only read.
Common mistakes
Treating arithmetic as bytes
Avoid
Prefer
The compiler already scales by sizeof(int). Adding it again moves too far.
Dereferencing one past the end
Avoid
Prefer
a + n is a valid address to compare with but not to read; <= reads beyond the array.
Important notes
- Pointer subtraction yields
ptrdiff_t(print with%td). - Arithmetic on
void *is not allowed in standard C (GCC permits it as an extension); cast tochar *for byte offsets.
Key takeaways
p + nmoves n elements;a[i]is*(a + i).- Array names decay to pointers to their first element.
- Stay within the array and its one-past-the-end address.
Try it yourself
- 1Reverse an array in place using two pointers that move toward each other.
- 2Write
count_char(const char *s, char c)using only pointer arithmetic (no indexing).
Finished reading? Mark it complete to update your dashboard.
Suggested practice
7 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- ModerateIf `int *p` holds address 1000 and int is 4 bytes, what address does `p + 1` hold?
- Moderate`a[i]` is defined in C as equivalent to…