C Notes
Learn pointer arithmetic in C including increment, decrement, addition, subtraction, pointer difference, and comparison with detailed memory diagrams and examples.
Pointer arithmetic is what makes C pointers truly powerful for array traversal and memory manipulation. Unlike regular arithmetic, pointer operations are automatically scaled by the size of the data type the pointer points to. This means incrementing an int* moves it forward by 4 bytes (not 1), making it naturally align with the next integer in memory.
How Pointer Arithmetic Works
When you perform arithmetic on a pointer, the compiler multiplies or divides by sizeof(pointed_type):
| p + 1 | address increases by 4 bytes |
| p + 3 | address increases by 12 bytes |
| p - 1 | address decreases by 4 bytes |
| c + 1 | address increases by 1 byte |
| c + 5 | address increases by 5 bytes |
| d + 1 | address increases by 8 bytes |
| d + 2 | address increases by 16 bytes |
Allowed Pointer Operations
| Operation | Allowed? | Description |
|---|---|---|
| pointer + integer | ✅ Yes | Move forward N elements |
| pointer - integer | ✅ Yes | Move backward N elements |
| pointer - pointer | ✅ Yes | Distance between elements |
| pointer ++ / -- | ✅ Yes | Move to next/previous element |
| pointer + pointer | ❌ No | Meaningless — two addresses |
| pointer * integer | ❌ No | Meaningless |
| pointer / integer | ❌ No | Meaningless |
Pointer Increment and Decrement
Initial: ptr = 0x7ffd12340010, *ptr = 10 After ptr++: ptr = 0x7ffd12340014, *ptr = 20 After ptr++: ptr = 0x7ffd12340018, *ptr = 30 After ptr--: ptr = 0x7ffd12340014, *ptr = 20
Memory Visualization
| Address | 0x010 0x014 0x018 0x01C 0x020 |
| Value | │ 10 │ 20 │ 30 │ 40 │ 50 │ |
| Value | │ 10 │ 20 │ 30 │ 40 │ 50 │ |
Pointer Addition and Subtraction
You can add or subtract integers to/from pointers:
*(ptr + 0) = 100 *(ptr + 1) = 200 *(ptr + 2) = 300 *(ptr + 4) = 500 *(end - 1) = 400 *(end - 3) = 200
Pointer Difference (Subtraction of Two Pointers)
Subtracting two pointers of the same type gives the number of elements between them:
p1 points to arr[1] at 0x7ffd3a1c0024 p2 points to arr[4] at 0x7ffd3a1c0030 p2 - p1 = 3 elements Byte difference = 12 bytes
The result is the number of elements (not bytes): (0x030 - 0x024) / sizeof(int) = 12 / 4 = 3.
Traversing Arrays with Pointer Arithmetic
This is the most practical application of pointer arithmetic:
Index notation: 5 15 25 35 45 55 Pointer notation: 5 15 25 35 45 55 Moving pointer: 5 15 25 35 45 55 Pointer compare: 5 15 25 35 45 55
Pointer Comparison
Pointers can be compared using relational operators (<, >, <=, >=, ==, !=):
start < end? Yes start == mid? No mid < end? Yes Elements: 10 20 30 40 50
Effect of Data Type on Arithmetic
Type | ptr | ptr+1 | Difference --------|----------|----------|---------- char* | 0x7ffd001 | 0x7ffd002 | 1 byte int* | 0x7ffd010 | 0x7ffd014 | 4 bytes double* | 0x7ffd020 | 0x7ffd028 | 8 bytes
Practical Example: String Length Using Pointer Arithmetic
Length of "Hello, Pointers!" = 16
Pre-increment vs Post-increment with Pointers
Be careful with operator precedence:
*p++ = 10 *p = 20 *++p = 30 ++*p = 31
| Expression | Meaning |
|---|---|
*p++ | Use value at p, then advance p |
*++p | Advance p, then use value at p |
++*p | Increment value at p (don't move p) |
(*p)++ | Use value at p, then increment value |
Interview Questions
Q1: What is the result of subtracting two pointers?
It gives the number of elements (not bytes) between them. The compiler divides the byte difference by sizeof(pointed_type). Both pointers must point to elements of the same array; otherwise, the behavior is undefined.
Q2: Why can't you add two pointers?
Adding two memory addresses produces a meaningless result — there's no logical interpretation for summing two locations. Subtraction gives a distance (meaningful), but addition gives no useful value.
**Q3: What does *(arr + i) mean and how does it relate to arr[i]?**
They are exactly equivalent. arr[i] is syntactic sugar for *(arr + i). The compiler computes the address arr + i * sizeof(type) and dereferences it. Interestingly, i[arr] is also valid C (because addition is commutative).
Q4: What happens if you increment a pointer past the end of an array?
Moving one past the last element is allowed (for loop termination comparisons), but dereferencing it is undefined behavior. Moving further beyond that is undefined even without dereferencing.
Summary
- Pointer arithmetic is scaled by
sizeof(pointed_type)—ptr+1advances by one element, not one byte - Only addition/subtraction with integers and subtraction between pointers are valid
- Pointer difference gives element count between two addresses in the same array
*p++dereferences first then advances;*++padvances first then dereferences- Pointer comparison works for pointers into the same array
- Array indexing
arr[i]is equivalent to*(arr + i)— pointer arithmetic under the hood
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Pointer Arithmetic in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, pointers, pointer, arithmetic, pointer arithmetic in c programming
Related C Programming Topics