Pointer Arithmetic
Adding an integer to a pointer moves it by that many elements, not by that many bytes. The result is only valid while it stays inside the array the pointer came from, or one position past its end.
Detailed Explanation
If p points at arr[0] and arr is an array of int, then p + 1 points at arr[1]. On a machine with four-byte int the numeric address went up by four, but you never wrote a four. The compiler scales by sizeof(*p) for you. That scaling is the entire point: arithmetic is in units of the pointed-to type, so the same loop body works whether you are walking bytes or 200-byte structs.
This is also the definition of subscripting. The standard defines arr[i] as *(arr + i). The array name decays to a pointer to its first element, i is scaled and added, and the result is dereferenced. A side effect of that symmetry is that arr[i] and i[arr] are the same expression. That is a curiosity, not a style to adopt.
Four operations are defined on pointers:
| Operation | Result | Notes |
|---|---|---|
p + n, p - n | pointer | moves n elements |
p2 - p1 | ptrdiff_t | element count, both must be in the same array |
p1 < p2, p1 == p2 | int | ordering only defined within one array |
++p, p += n | pointer | same scaling rules |
The rule that matters most is about range. A pointer arithmetic result is valid only if it lands inside the array object the original pointer points into, or exactly one element past the last one. That one-past-the-end pointer is legal to form and legal to compare, which is what makes the standard idiom for (int *p = arr; p != arr + n; ++p) correct. Dereferencing it is undefined behavior.
Everything outside that window is undefined behavior at the moment the pointer is computed, before you dereference anything. Writing arr - 1 to set up a loop that will immediately increment is already undefined, even though nothing is read. This is not pedantry: compilers use the rule to prove loops terminate and to vectorize them, and code that violates it can be optimized into something you did not write. On a machine with segmented addressing, computing an out-of-range address could trap in hardware.
A single object counts as an array of one for this purpose, so &x + 1 is a valid one-past-the-end pointer.
Two more sharp edges. Comparing pointers into different objects with < or > is unspecified — the comparison produces some answer and the standard does not say which. Equality comparison between unrelated pointers is well defined, and says they are unequal. And arithmetic on void * is not standard C at all, because void has no size; GCC and Clang allow it as an extension that treats void as one byte, but portable code casts to unsigned char * first.
Subtraction yields ptrdiff_t, a signed type from <stddef.h>, printed with %td. Do not assign it to an int in code that might handle arrays larger than INT_MAX elements.
When would a learner reach for this? Parsing a buffer byte by byte, walking a string until the terminating '\0', implementing memcpy, or splitting an array in a binary search. Modern style often prefers indices because they are easier to bounds-check and read, and on current compilers there is no measurable speed difference. Pick one per function and stay with it.
Code Examples
#include <stddef.h>
#include <stdio.h>
int main(void)
{
int arr[5] = { 10, 20, 30, 40, 50 };
int *begin = arr; /* array decays to &arr[0] */
int *end = arr + 5; /* one past the end: legal to form and compare,
undefined to dereference */
/* The canonical walk. p != end is the guard, not p < end - 1. */
for (int *p = begin; p != end; ++p) {
printf("%d ", *p); /* prints: 10 20 30 40 50 */
}
putchar('\n');
ptrdiff_t count = end - begin; /* difference is an element count, not bytes */
printf("%td elements\n", count); /* prints: 5 elements */
/* arr[i] is defined as *(arr + i): the compiler scales i by sizeof(int). */
printf("%d %d\n", arr[3], *(arr + 3)); /* prints: 40 40 */
return 0;
}#include <stdio.h>
#include <string.h>
/* Splitting a buffer with two pointers: a common real use of the arithmetic.
Returns the number of bytes copied into out, never exceeding out_size - 1. */
static size_t copy_first_word(const char *text, char *out, size_t out_size)
{
if (out_size == 0) {
return 0;
}
const char *start = text;
while (*start == ' ') { /* stops at '\0', which is not a space */
++start;
}
const char *stop = start;
while (*stop != '\0' && *stop != ' ') {
++stop;
}
size_t len = (size_t)(stop - start); /* pointer difference: a length */
if (len > out_size - 1) {
len = out_size - 1; /* leave room for the terminator */
}
memcpy(out, start, len);
out[len] = '\0';
return len;
}
int main(void)
{
char word[8];
copy_first_word(" pointer arithmetic", word, sizeof word);
printf("[%s]\n", word); /* prints: [pointer] */
return 0;
}Common Gotchas
Forming a pointer outside `[begin, end]` is undefined behavior even without dereferencing it
`int *p = arr - 1;` is already wrong, so the descending loop `for (p = arr + n - 1; p >= arr - 1; --p)` is broken no matter how reliably it appears to work. Rewrite it as `for (p = arr + n; p-- != arr; )`.
Comparing pointers into unrelated objects with `<` is unspecified
it will not crash, but the answer carries no information, so it cannot be used to test "is this pointer inside my buffer". Equality comparison is fine and answers "not equal".
Pointer subtraction yields `ptrdiff_t`, printed with `%td`
assigning it to an `int` silently truncates on very large arrays, and passing it to `printf` as `%d` is undefined behavior on most 64-bit platforms.