Introduction
Pointer arithmetic is the process of performing arithmetic operations on pointers. Unlike ordinary integer arithmetic, pointer arithmetic operates on memory addresses and automatically takes the size of the pointed-to data type into account.
Pointer arithmetic is one of the most powerful features of C. It allows programs to efficiently traverse arrays, process strings, manipulate buffers, and implement many low-level algorithms.
To use pointer arithmetic safely, you must understand how pointers move through memory and the rules defined by the C language.
What Is Pointer Arithmetic?
Pointer arithmetic refers to adding, subtracting, incrementing, decrementing, or comparing pointers.
Unlike integer variables, pointer arithmetic is based on the size of the object being pointed to rather than individual bytes.
Syntax
pointer + n
pointer - n
pointer1 - pointer2
++pointer
--pointer
How Pointer Arithmetic Works
Suppose an int occupies 4 bytes on your platform.
If a pointer stores the following address:
Example
Address: 1000
Type: int *
Then:
| Expression | New Address |
|---|---|
ptr |
1000 |
ptr + 1 |
1004 |
ptr + 2 |
1008 |
ptr + 3 |
1012 |
The compiler automatically multiplies the offset by the size of the pointed-to type.
Incrementing a Pointer
The increment operator moves a pointer to the next object of the same type.
Example
#include <stdio.h>
int main(void)
{
int numbers[] = {10, 20, 30};
int *ptr = numbers;
printf("%d\n", *ptr);
ptr++;
printf("%d\n", *ptr);
return 0;
}
Output
10
20
After incrementing, the pointer refers to the next array element.
Decrementing a Pointer
The decrement operator moves a pointer to the previous object.
Example
int numbers[] = {10, 20, 30};
int *ptr = &numbers[2];
ptr--;
printf("%d\n", *ptr);
The pointer moves from the third element to the second element.
Adding an Integer to a Pointer
An integer can be added to a pointer to move forward multiple objects.
Example
int values[] = {5, 10, 15, 20};
int *ptr = values;
ptr = ptr + 2;
printf("%d\n", *ptr);
Output
15
Subtracting an Integer from a Pointer
Subtracting an integer moves a pointer backward.
Example
int values[] = {5, 10, 15, 20};
int *ptr = &values[3];
ptr = ptr - 2;
printf("%d\n", *ptr);
Output
10
Subtracting Two Pointers
Two pointers that point into the same array can be subtracted.
The result is the number of elements between them, not the number of bytes.
Example
#include <stdio.h>
int main(void)
{
int values[] = {10, 20, 30, 40};
int *first = &values[0];
int *last = &values[3];
printf("%td\n", last - first);
return 0;
}
Output
3
ptrdiff_t, which is typically printed using the %td format specifier.
Comparing Pointers
Pointers that refer to elements of the same array can be compared.
Example
if (ptr1 < ptr2)
{
printf("ptr1 comes first.\n");
}
Pointer comparisons are commonly used when traversing arrays.
Pointer Arithmetic with Arrays
Array indexing is implemented using pointer arithmetic.
The following expressions are equivalent:
| Array Expression | Pointer Expression |
|---|---|
array[0] |
*(array + 0) |
array[1] |
*(array + 1) |
array[i] |
*(array + i) |
This relationship explains why arrays and pointers are closely related in C.
Traversing an Array Using Pointer Arithmetic
Example
#include <stdio.h>
int main(void)
{
int numbers[] = {10, 20, 30, 40};
int *ptr = numbers;
while (ptr < numbers + 4)
{
printf("%d ", *ptr);
ptr++;
}
return 0;
}
Output
10 20 30 40
Pointer Arithmetic Depends on the Data Type
The amount by which a pointer moves depends on its base type.
| Pointer Type | Increment Size |
|---|---|
char * |
1 byte |
short * |
sizeof(short) |
int * |
sizeof(int) |
double * |
sizeof(double) |
struct Student * |
sizeof(struct Student) |
The compiler automatically performs these calculations.
Invalid Pointer Arithmetic
Not every arithmetic operation is permitted on pointers.
Examples of invalid operations include:
Example
ptr1 + ptr2;
ptr * 2;
ptr / 2;
ptr % 2;
Only addition and subtraction with integers, subtraction between compatible pointers into the same array, and pointer comparisons are supported.
One-Past-the-End Pointer
C allows a pointer to point one element past the end of an array. Such a pointer is useful for comparisons and loop termination.
Example
int values[5];
int *begin = values;
int *end = values + 5;
The pointer end may be compared with other pointers, but it must never be dereferenced.
Common Beginner Mistakes
- Assuming pointer arithmetic always moves one byte.
- Dereferencing a pointer after moving beyond the array.
- Subtracting pointers that refer to different arrays.
- Adding two pointers together.
- Ignoring that pointer arithmetic depends on the pointed-to type.
Best Practices
- Perform pointer arithmetic only within the same array.
- Prefer array indexing when it improves readability.
- Always ensure pointers remain within valid array bounds.
- Understand that the compiler scales pointer arithmetic automatically.
- Use pointer arithmetic carefully in low-level code where it provides clear advantages.
Summary
Pointer arithmetic allows pointers to move efficiently through memory while respecting the size of the objects they reference. It is widely used for array traversal, string processing, and many systems programming tasks.
| Operation | Description |
|---|---|
ptr + n |
Move forward n elements. |
ptr - n |
Move backward n elements. |
ptr1 - ptr2 |
Compute the number of elements between two pointers. |
++ptr |
Move to the next element. |
--ptr |
Move to the previous element. |
ptr1 < ptr2 |
Compare pointers within the same array. |
Mastering pointer arithmetic is essential for understanding arrays, strings, dynamic memory allocation, iterators, and many advanced programming techniques in C.