Pointer arithmetic lets you move a pointer through memory in units of the type it points to. Adding 1 to an int * advances it by sizeof(int) bytes, so the pointer lands on the next int. It is how functions like memchr() return a position inside a buffer.
Remember
p + n pointer n elements after p
p - n pointer n elements before p
p - q number of elements between p and q (type ptrdiff_t)
p++ / p-- step to the next / previous element
p < q compare positions (same array only)
address(p + n) = address(p) + n * sizeof(*p)
Operation
Allowed?
Result
pointer + integer
Yes
Pointer, scaled by element size
pointer - integer
Yes
Pointer, scaled by element size
pointer - pointer
Yes (same array)
ptrdiff_t element count
pointer + pointer
No
Compile error
pointer * / /
No
Compile error
Approach
How to Use Pointer Arithmetic in C
Point at an element of an array, then add or subtract integers while staying inside the array bounds.
Point at an element: int *p = arr; (an array name decays to a pointer to its first element).
Move with p + i, p++, or p -= 2; the compiler scales by sizeof(*p).
Read or write through the pointer with *(p + i), which is the same as p[i].
Subtract two pointers into the same array to get an element count or index.
Compile with gcc file.c -std=c11 -o out and run.
Type of p
p + 1 moves by
Notes
char *
1 byte
Used for raw byte offsets
int *
sizeof(int) (usually 4)
Next integer
double *
sizeof(double) (usually 8)
Next double
void *
Not defined in standard C
Cast to unsigned char * first
Pseudocode
Pseudocode
p = address of arr[0]
q = p + 2 // address of arr[2]
value = *q // read arr[2]
count = q - p // 2 elements
stay within arr[0] .. arr[n] // one past the end is the last legal address
Reference
Cheat Sheet
Keep these rules close when you walk memory with pointers.
Topic
Detail
Scaling
p + n adds n * sizeof(*p) bytes
Subscript
p[i] is exactly *(p + i)
Difference type
ptrdiff_t from <stddef.h>; print with %td
Legal range
First element up to one past the last element
Dereference limit
Never dereference the one-past-the-end pointer
Different arrays
Subtracting or comparing them is undefined behavior
Trace
Worked Walkthrough — Array of Four ints
Assume int arr[4] = {10, 20, 30, 40}; starts at address 1000 and sizeof(int) is 4.
Calculation
p = arr -> address 1000 *p = 10
p + 1 -> address 1004 *(p + 1) = 20
p + 2 -> address 1008 *(p + 2) = 30
p + 3 -> address 1012 *(p + 3) = 40
p + 4 -> address 1016 one past the end (compare only)
(p + 3) - p = 3 elements (12 bytes apart, divided by sizeof(int))
Code
C Examples
Compile with gcc file.c -std=c11 -o out. See also stddef.h for ptrdiff_t and string.h for pointer-returning functions.
Example 1 — Walk an Array with a Pointer
Add the loop counter to the base pointer to reach each element.
C
#include <stdio.h>
int main(void) {
int a[] = { 10, 20, 30, 40 };
int *p = a;
size_t n = sizeof a / sizeof a[0];
for (size_t i = 0; i < n; i++) {
printf("%d ", *(p + i));
}
printf("\n");
return 0;
}
Output
10 20 30 40
How It Works
Base pointer.p points at a[0].
Element count.sizeof a / sizeof a[0] gives 4.
Offset.p + i is the address of a[i].
Dereference.*(p + i) reads the value.
Example 2 — Turn a memchr() Result into an Index
Library functions return pointers. Subtract the start pointer to get a zero-based index.
Search.memchr returns a pointer to the first match or NULL.
Check for NULL. Never subtract from a null pointer.
Subtract.comma - s counts char elements between them.
Print with %td. That is the format for ptrdiff_t.
Example 3 — See the Scaling by Element Size
Pointer difference counts elements; casting to char * exposes the byte distance.
C
#include <stddef.h>
#include <stdio.h>
int main(void) {
int arr[3] = { 1, 2, 3 };
int *p = arr;
int *q = p + 2;
printf("q - p = %td elements\n", q - p);
printf("byte distance = %td\n", (char *)q - (char *)p);
return 0;
}
Output (when sizeof(int) is 4)
q - p = 2 elements
byte distance = 8
How It Works
Two pointers.q points two int elements past p.
Element difference.q - p is 2, not 8.
Byte difference. Casting both to char * makes each step 1 byte.
Takeaway. The compiler divides or multiplies by sizeof(*p) for you.
Edge Cases & Pitfalls
Pointer arithmetic is easy to misuse. Check these cases.
bounds
Going out of bounds
Computing a pointer before the first element or beyond one-past-the-end is undefined behavior, even without dereferencing it.
end
Dereferencing one past the end
arr + n is a valid pointer for comparisons, but *(arr + n) reads memory you do not own.
arrays
Mixing different arrays
Subtracting or comparing pointers from different arrays has no defined meaning.
void
Arithmetic on void *
Standard C forbids it. GCC allows it as an extension (treating size as 1), which is not portable. Cast to unsigned char *.
format
Wrong printf format
Use %td for ptrdiff_t and %zu for size_t; %d can misprint on 64-bit builds.
Analysis
Performance Notes
Style
Cost
Notes
p[i]
O(1) per access
Same as *(p + i)
*p++ loop
O(1) per step
Often the same machine code at -O2
Pointer difference
O(1)
One subtraction and a divide by element size
Modern compilers generate equivalent code for index and pointer loops, so choose the form that reads best. Pointer arithmetic matters most when a library function hands you a position.
Remember
Key Takeaways
Scaling:p + n moves n elements, not n bytes.
Subscript:p[i] and *(p + i) are the same.
Difference: subtract pointers from one array to get an index, as ptrdiff_t.
Limits: stay between the first element and one past the last.
One line: pointer arithmetic counts in elements, and the compiler handles the byte math.
Frequently Asked Questions
It is adding or subtracting integers to a pointer, or subtracting two pointers, with results scaled by the size of the pointed-to type.
Pointer arithmetic is defined in elements. The compiler multiplies the integer by sizeof(*p) so p + 1 lands on the next element.
No. Adding pointers has no meaning and is a compile error. You can subtract two pointers into the same array.
ptrdiff_t, a signed integer type from <stddef.h>. Print it with the %td format.
Not in standard C. GCC accepts it as an extension, but portable code casts to unsigned char * or char * first.
Standard C allows a pointer to the element just after the last one, for comparisons and loop bounds. You must not dereference it.