Pointer Arithmetic in C
Add and subtract with pointers! Learn how pointer math works with arrays and why ptr++ moves by the size of the data type.
What You Will Learn
- ✓Understand pointer increment/decrement
- ✓Know why ptr++ moves by sizeof(type)
- ✓Use pointers to traverse arrays
- ✓Calculate pointer differences
01What is Pointer Arithmetic?
➕ Simple Definition
Pointer arithmetic lets you perform math operations on pointers. When you add 1 to a pointer, it moves to the next element, not the next byte!
🔑 The Key Rule
ptr + 1 = ptr + sizeof(*ptr)
Adding 1 moves by the SIZE of the data type, not 1 byte!
| Data Type | Size | ptr + 1 moves by |
|---|---|---|
| char* | 1 byte | 1 byte |
| int* | 4 bytes | 4 bytes |
| double* | 8 bytes | 8 bytes |
02Pointer Arithmetic Operations
| Operation | Syntax | Description |
|---|---|---|
| Increment | ptr++ | Move to next element |
| Decrement | ptr-- | Move to previous element |
| Addition | ptr + n | Move n elements forward |
| Subtraction | ptr - n | Move n elements backward |
| Difference | ptr1 - ptr2 | Number of elements between |
1#include <stdio.h>23int main() {4 int arr[] = {10, 20, 30, 40, 50};5 int* ptr = arr; // Points to first element6 7 printf("Array: ");8 for (int i = 0; i < 5; i++) {9 printf("%d ", arr[i]);10 }11 printf("\n\n");12 13 // ptr points to arr[0]14 printf("*ptr = %d (element at ptr)\n", *ptr);15 16 // ptr + 1 points to arr[1]17 printf("*(ptr + 1) = %d (next element)\n", *(ptr + 1));18 19 // ptr + 2 points to arr[2]20 printf("*(ptr + 2) = %d (2nd next)\n", *(ptr + 2));21 22 // Increment pointer23 ptr++; // Now points to arr[1]24 printf("\nAfter ptr++:\n");25 printf("*ptr = %d\n", *ptr);26 27 // Decrement pointer28 ptr--; // Back to arr[0]29 printf("\nAfter ptr--:\n");30 printf("*ptr = %d\n", *ptr);31 32 // Pointer difference33 int* start = &arr[0];34 int* end = &arr[4];35 printf("\nElements between start and end: %ld\n", end - start);36 37 return 0;38}Array: 10 20 30 40 50
*ptr = 10 (element at ptr)
*(ptr + 1) = 20 (next element)
*(ptr + 2) = 30 (2nd next)
After ptr++:
*ptr = 20
After ptr--:
*ptr = 10
Elements between start and end: 4
03Arrays and Pointers Relationship
🔗 Key Insight
Array name is a pointer to the first element. These are equivalent:
arr[i]
Array notation
*(arr + i)
Pointer notation
1#include <stdio.h>23int main() {4 int arr[] = {10, 20, 30, 40, 50};5 6 printf("Array notation vs Pointer notation:\n\n");7 8 for (int i = 0; i < 5; i++) {9 printf("arr[%d] = %d | *(arr + %d) = %d\n", 10 i, arr[i], i, *(arr + i));11 }12 13 printf("\nAddresses:\n");14 for (int i = 0; i < 5; i++) {15 printf("&arr[%d] = %p | arr + %d = %p\n", 16 i, (void*)&arr[i], i, (void*)(arr + i));17 }18 19 return 0;20}Array notation vs Pointer notation:
arr[0] = 10 | *(arr + 0) = 10
arr[1] = 20 | *(arr + 1) = 20
arr[2] = 30 | *(arr + 2) = 30
arr[3] = 40 | *(arr + 3) = 40
arr[4] = 50 | *(arr + 4) = 50
04Practical: Iterating with Pointers
1#include <stdio.h>23// Sum array using pointer arithmetic4int sumArray(int* arr, int size) {5 int sum = 0;6 int* end = arr + size; // Pointer to one past last element7 8 while (arr < end) {9 sum += *arr; // Add current element10 arr++; // Move to next11 }12 return sum;13}1415// Reverse array in place using pointers16void reverseArray(int* arr, int size) {17 int* start = arr;18 int* end = arr + size - 1;19 20 while (start < end) {21 // Swap22 int temp = *start;23 *start = *end;24 *end = temp;25 26 start++;27 end--;28 }29}3031int main() {32 int numbers[] = {1, 2, 3, 4, 5};33 int size = 5;34 35 printf("Original: ");36 for (int i = 0; i < size; i++) printf("%d ", numbers[i]);37 printf("\n");38 39 printf("Sum: %d\n", sumArray(numbers, size));40 41 reverseArray(numbers, size);42 printf("Reversed: ");43 for (int i = 0; i < size; i++) printf("%d ", numbers[i]);44 printf("\n");45 46 return 0;47}Original: 1 2 3 4 5
Sum: 15
Reversed: 5 4 3 2 1
05Common Mistakes
❌ Going out of bounds
int arr[5]; int* ptr = arr + 10;
❌ Adding two pointers
int* p3 = p1 + p2;
int diff = p2 - p1;
06Summary
🎯 Key Takeaways
- •ptr + n: Moves by n * sizeof(type) bytes
- •arr[i] ≡ *(arr + i): Array and pointer notation are equivalent
- •ptr1 - ptr2: Number of elements between pointers
- •Use cases: Efficient array iteration, string operations
08Next Steps
Learn how C programs are organized in memory: