Pointer Arithmetic in C
If a pointer holds address 1000, what is p + 1? Most students say 1001. For an int pointer, the answer is usually 1004!
In this lesson you will learn pointer arithmetic: how pointers move with ++, --, + and -, how to subtract and compare pointers, and which operations are not allowed. This is the key to walking through arrays with pointers.
What You Will Learn
- Why
p + 1moves by the size of the type. - Increment and decrement of pointers.
- Adding or subtracting an integer.
- Subtracting two pointers.
- Comparing pointers.
- Which pointer operations are invalid.
Pointer Arithmetic Explained
Exam definition: Pointer arithmetic is performing arithmetic on pointers. When an integer n is added to a pointer, the address increases by n × sizeof(type), so the pointer moves n elements, not n bytes. Allowed operations: pointer ± integer, ++, --, pointer − pointer (same array) and comparison.
Memory trick: A pointer counts in steps, not in bytes, like walking on stair steps: p + 1 means "next step", and the step size is the type size (1 for char, 4 for int, 8 for double).
int a[4] = {10, 20, 30, 40}; int *p = a; (int = 4 bytes, a starts at 1000)
address : 1000 1004 1008 1012
value : 10 20 30 40
pointer : p p+1 p+2 p+3
New address = old address + n x sizeof(int)
p + 2 = 1000 + 2 x 4 = 1008 -> *(p + 2) = 30
Syntax of Pointer Operations
p++; /* move to the next element */
p--; /* move to the previous element */
p = p + 3; /* move 3 elements forward */
q - p /* number of elements between q and p (same array) */
p < q /* is p before q? (same array) */
| Operation | Allowed? | Result |
|---|---|---|
p + n, p - n | Yes | A pointer n elements away |
p++, p--, ++p, --p | Yes | Next / previous element |
q - p | Yes (same array) | An integer: elements between them |
p == q, p < q, … | Yes (same array, or with NULL for ==) | True / false |
p + q | No | Compile error: adding addresses is meaningless |
p * 2, p / 2, p % 2 | No | Compile error |
Think of house numbers: "house 40 minus house 32" (how far apart) makes sense. "House 40 plus house 32" makes no sense.
Example 1: How p + 1 Moves for Different Types
#include <stdio.h>
int main(void)
{
char c[3];
int i[3];
double d[3];
char *pc = c;
int *pi = i;
double *pd = d;
/* bytes moved = (address after) - (address before), measured in bytes */
printf("char* p+1 moves %d byte(s)\n", (int)((char *)(pc + 1) - (char *)pc));
printf("int* p+1 moves %d byte(s)\n", (int)((char *)(pi + 1) - (char *)pi));
printf("double* p+1 moves %d byte(s)\n", (int)((char *)(pd + 1) - (char *)pd));
printf("int* p+2 moves %d byte(s)\n", (int)((char *)(pi + 2) - (char *)pi));
return 0;
}
Output
char* p+1 moves 1 byte(s) int* p+1 moves 4 byte(s) double* p+1 moves 8 byte(s) int* p+2 moves 8 byte(s)
Code Explanation
- Real addresses change every run, so instead of printing them we print how many bytes the pointer moved.
- Casting both addresses to
char *and subtracting gives the distance in bytes (a char is 1 byte). pc + 1moves 1 byte,pi + 1moves 4 bytes,pd + 1moves 8 bytes: always sizeof(type).pi + 2moves 2 × 4 = 8 bytes: two int steps.
How Pointer Subtraction and Comparison Work
q - pgives the number of elements, not bytes. Ifp = &a[1]andq = &a[4], thenq - pis 3.- The result type is
ptrdiff_t(a signed integer). Print it with%td, or cast toint. - Comparison (
<,>) tells which element comes first. It is meaningful only for pointers into the same array. - A pointer may point one past the last element (like
a + n) for comparisons, but you must not dereference it.
Example 2: Walk an Array with p++ and Find an Element's Position
#include <stdio.h>
int main(void)
{
int a[] = {15, 42, 8, 23, 4};
int n = sizeof(a) / sizeof(a[0]);
int *p, *start = a, *end = a + n; /* end = one past the last */
int *big = a;
printf("Forward :");
for (p = start; p < end; p++) /* compare pointers */
{
printf(" %d", *p);
if (*p > *big)
big = p;
}
printf("\n");
printf("Backward:");
for (p = end - 1; p >= start; p--)
printf(" %d", *p);
printf("\n");
printf("Largest %d is at index %d\n", *big, (int)(big - start));
printf("Elements between first and last: %d\n", (int)((end - 1) - start));
return 0;
}
Output
Forward : 15 42 8 23 4 Backward: 4 23 8 42 15 Largest 42 is at index 1 Elements between first and last: 4
p < endkeeps the loop inside the array;p++steps one int each time.big - startis pointer subtraction: it gives the index of the largest element.- The backward loop starts at
end - 1, the last real element.
Also see max and min element in an array using pointer.
*p++ vs (*p)++ vs ++*p
These look alike but do different things. ++ and * have the same precedence and group right to left.
| Expression | Means | Changes |
|---|---|---|
*p++ | *(p++): use *p, then move p | The pointer |
(*p)++ | Increase the value at p (after use) | The value |
++*p | ++(*p): increase the value, then use it | The value |
*++p | Move p first, then use the new *p | The pointer |
Important Points to Remember
- p + n moves n × sizeof(type) bytes.
- Allowed: ± integer, ++/--, pointer − pointer, comparison.
- Not allowed: pointer + pointer, multiply, divide, modulus.
q - pgives elements, not bytes.- Subtract and compare only pointers into the same array.
*p++moves the pointer;(*p)++changes the value.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Thinking p + 1 adds 1 byte | Wrong answers in address questions | It adds sizeof(type) |
p + q | Compile error | Use q - p for a distance |
Writing *p++ to increase the value | Pointer moves instead | (*p)++ |
Loop p <= a + n | Reads one past the end | p < a + n |
| Subtracting pointers from different arrays | Undefined behaviour | Only within one array |
Pointer Arithmetic Practice Questions
- An
int *pholds 2000 and int is 4 bytes. What isp + 3? - A
double *qholds 5000. What isq - 1? - If
p = &a[2]andq = &a[7], what isq - p? - Why is adding two pointers not allowed?
- What is the difference between
*p++and(*p)++?
Show answers
- 2000 + 3 × 4 = 2012.
- 5000 − 8 = 4992.
- 5.
- The sum of two addresses doesn't point to anything meaningful.
*p++reads the value then moves the pointer;(*p)++increases the value and leaves the pointer.
Coding Exercise
Using only a pointer (no a[i]), find the sum of the array {3, 6, 9, 12, 15} and add 1 to every element. Print the sum and the new array.
Show solution
#include <stdio.h>
int main(void)
{
int a[] = {3, 6, 9, 12, 15};
int *p, sum = 0;
for (p = a; p < a + 5; p++)
{
sum += *p;
(*p)++; /* increase the value, not the pointer */
}
printf("Sum = %d\nNew :", sum);
for (p = a; p < a + 5; p++)
printf(" %d", *p);
printf("\n");
return 0;
}
Sum = 45 New : 4 7 10 13 16
Quick Quiz: Pointer Arithmetic (MCQ)
-
char *cholds 3000.c + 5is:A) 3005 B) 3020 C) 3040 D) 3001
-
Which operation is invalid?
A)
p + 2B)p - qC)p + qD)p++ -
q - pfor two int pointers 3 elements apart gives:A) 12 B) 3 C) 4 D) 1
-
int a[] = {5, 9}; int *p = a; int x = *p++;Now x and *p are:A) 5, 9 B) 6, 9 C) 9, 9 D) 5, 5
-
int a[] = {5, 9}; int *p = a; (*p)++;Now a[0] is:A) 5 B) 6 C) 9 D) 10
Show answers
- A: 3005 (char is 1 byte).
- C:
p + q. - B: 3 elements.
- A: x = 5, then p points to 9.
- B: 6.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| p + n | Address + n × sizeof(type) |
|---|---|
| int* p + 1 | Moves 4 bytes (int = 4) |
| q − p | Number of elements between |
| Valid | ±int, ++, −−, ptr − ptr, compare |
| Invalid | ptr + ptr, ×, ÷, % |
| *p++ | Use value, then move pointer |
| (*p)++ | Increase value |
Frequently asked questions
- What happens when you increment a pointer?
- It moves to the next element: the address grows by sizeof(type).
- Can two pointers be added?
- No. Only subtraction of two pointers (same array) is allowed.
- What does subtracting two pointers give?
- The number of elements between them, not bytes.
Remember
- Pointers walk in steps, step size = type size.
- House numbers: subtract yes, add no.
Next Topic
Next lesson: Pointers and Arrays in C. You will see why an array name behaves like a pointer and why a[i] equals *(a + i).