Pointer arithmetic in C means adding or subtracting an integer from a pointer, or subtracting two pointers, with the result scaled by the size of the type being pointed to. Adding 1 to an int * moves it forward by sizeof(int) bytes (usually 4), not by one byte. The rules are only defined inside one array object, plus the position one past its last element. If you searched for "pointer arithmetic in c", this guide covers the valid operations, how scaling works, and the traps that lead to undefined behaviour.
You should already know how to declare and dereference a pointer.
When you write p + n, the compiler computes the address p + n * sizeof(*p). That is why pointer arithmetic lands on whole elements of the array:
The expression a[i] is defined by the standard as *(a + i), so indexing and pointer arithmetic are the same operation written two ways.
| Expression | Meaning |
|---|---|
p + n, p - n | Pointer moved by n elements |
p++, p--, ++p, --p | Move by one element |
p += n, p -= n | Move by n elements, in place |
p2 - p1 | Number of elements between two pointers, type ptrdiff_t |
p1 < p2, p1 == p2 | Comparison, meaningful for pointers into the same array |
You cannot add two pointers, multiply or divide a pointer, and you cannot do arithmetic on a void * in standard C (GCC allows it as an extension that treats the size as 1; do not rely on it).
A common idiom is to loop with a pointer from the first element up to the end position:
a + n points one element past the last item. That position is valid to compute and compare, but you must never dereference it.
Subtracting two pointers into the same array gives the number of elements between them, with type ptrdiff_t from <stddef.h>. Print it with %td:
Because the difference is scaled by element size, it is a count of elements, not bytes. For char, the two are equal since sizeof(char) is 1.
The standard defines arithmetic only within one array object and one position past its end. Anything else is undefined behaviour, even if you never dereference the result:
&x + 1 for a single variable (one past a single object is allowed, further is not).Sanitizers help here. Compile with -fsanitize=address,undefined in GCC or Clang to catch out-of-range accesses at run time.
The step always depends on the pointer type, which matters when you cast:
Casting to a character type lets you inspect individual bytes of an object. Casting between other unrelated pointer types and then dereferencing usually violates strict aliasing rules.
C scales arithmetic by the size of the pointed-to type so that p + 1 always refers to the next element. Cast to char * if you need byte steps.
No. The result is undefined. Pointer subtraction is only meaningful for two pointers into the same array object.
ptrdiff_t, a signed integer type defined in <stddef.h>. Use %td with printf.
Yes. The language defines the subscript operator that way, which is why even i[p] compiles, though nobody should write it.
Pointer arithmetic moves by whole elements, because the compiler multiplies by the element size. Keep every pointer inside its array or at the one-past-the-end position, subtract only pointers from the same array, and use ptrdiff_t for distances. Anything outside those rules is undefined behaviour.