Chapter 21Advanced

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.

18 min readUpdated 2024-12-16
pointer arithmeticptr++ptr--pointer subtractionarray pointer

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 TypeSizeptr + 1 moves by
char*1 byte1 byte
int*4 bytes4 bytes
double*8 bytes8 bytes

02Pointer Arithmetic Operations

OperationSyntaxDescription
Incrementptr++Move to next element
Decrementptr--Move to previous element
Additionptr + nMove n elements forward
Subtractionptr - nMove n elements backward
Differenceptr1 - ptr2Number of elements between
pointer_arithmetic.c
C
1#include <stdio.h>
2
3int main() {
4 int arr[] = {10, 20, 30, 40, 50};
5 int* ptr = arr; // Points to first element
6
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 pointer
23 ptr++; // Now points to arr[1]
24 printf("\nAfter ptr++:\n");
25 printf("*ptr = %d\n", *ptr);
26
27 // Decrement pointer
28 ptr--; // Back to arr[0]
29 printf("\nAfter ptr--:\n");
30 printf("*ptr = %d\n", *ptr);
31
32 // Pointer difference
33 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}
Output

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

array_pointer_relation.c
C
1#include <stdio.h>
2
3int 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}
Output

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

pointer_iteration.c
C
1#include <stdio.h>
2
3// Sum array using pointer arithmetic
4int sumArray(int* arr, int size) {
5 int sum = 0;
6 int* end = arr + size; // Pointer to one past last element
7
8 while (arr < end) {
9 sum += *arr; // Add current element
10 arr++; // Move to next
11 }
12 return sum;
13}
14
15// Reverse array in place using pointers
16void reverseArray(int* arr, int size) {
17 int* start = arr;
18 int* end = arr + size - 1;
19
20 while (start < end) {
21 // Swap
22 int temp = *start;
23 *start = *end;
24 *end = temp;
25
26 start++;
27 end--;
28 }
29}
30
31int 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}
Output

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: