Pointer arithmetic in C refers to the manipulation of pointers using arithmetic operations.
It allows you to navigate through arrays and manipulate memory addresses. Pointer arithmetic operates on data types and scales operations based on the size of the data type the pointer references.
1. Basics of Pointer Arithmetic
In C, pointers can be incremented, decremented, and subtracted, allowing you to move between memory locations.
The arithmetic operations adjust based on the size of the data type the pointer is referencing.
2. Incrementing a Pointer
When you increment a pointer, it points to the next memory location of the data type it references. For example, if you have an int pointer and increment it, it will move to the next int (typically 4 bytes forward).
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40}; int *ptr = arr; printf("Initial address: %p, Value: %d\n", (void*)ptr, *ptr); ptr++; // Move to the next element printf("After incrementing, address: %p, Value: %d\n", (void*)ptr, *ptr); return 0; }
Explanation:
- ptr initially points to the first element of arr.
- ptr++ increments the pointer to point to the next int (4 bytes forward).
Output:
Initial address: 0x... (address of arr[0]), Value: 10 After incrementing, address: 0x... (address of arr[1]), Value: 20
3. Decrementing a Pointer
When you decrement a pointer, it moves to the previous memory location of its data type.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40}; int *ptr = &arr[2]; // Start at arr[2] printf("Initial address: %p, Value: %d\n", (void*)ptr, *ptr); ptr--; // Move to the previous element printf("After decrementing, address: %p, Value: %d\n", (void*)ptr, *ptr); return 0; }
Explanation:
- ptr initially points to arr[2].
- ptr– moves the pointer back by one int (4 bytes), so it now points to arr[1].
Output:
Initial address: 0x... (address of arr[2]), Value: 30 After decrementing, address: 0x... (address of arr[1]), Value: 20
4. Adding an Integer to a Pointer
You can add an integer value to a pointer, which moves the pointer by that number of elements.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *ptr = arr; printf("Initial address: %p, Value: %d\n", (void*)ptr, *ptr); ptr = ptr + 3; // Move 3 elements forward printf("After adding 3, address: %p, Value: %d\n", (void*)ptr, *ptr); return 0; }
Explanation:
- ptr + 3 moves ptr by three int elements (12 bytes), so it now points to arr[3].
Output:
Initial address: 0x... (address of arr[0]), Value: 10 After adding 3, address: 0x... (address of arr[3]), Value: 40
5. Subtracting an Integer from a Pointer
You can subtract an integer value from a pointer, moving it back by that number of elements.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *ptr = &arr[4]; // Start at arr[4] printf("Initial address: %p, Value: %d\n", (void*)ptr, *ptr); ptr = ptr - 2; // Move 2 elements back printf("After subtracting 2, address: %p, Value: %d\n", (void*)ptr, *ptr); return 0; }
Explanation:
- ptr – 2 moves ptr back by two int elements (8 bytes), so it now points to arr[2].
Output:
Initial address: 0x... (address of arr[4]), Value: 50 After subtracting 2, address: 0x... (address of arr[2]), Value: 30
6. Pointer Difference
You can calculate the difference between two pointers, which gives the number of elements between them.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *ptr1 = &arr[1]; int *ptr2 = &arr[4]; int diff = ptr2 - ptr1; printf("Difference between ptr2 and ptr1: %d elements\n", diff); return 0; }
Explanation:
- ptr2 – ptr1 calculates the number of int elements between ptr1 and ptr2, which is 3.
Output:
Difference between ptr2 and ptr1: 3 elements
7. Iterating Over an Array with Pointer Arithmetic
Using pointer arithmetic, you can iterate over an array by incrementing a pointer.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *ptr = arr; for (int i = 0; i < 5; i++) { printf("Element %d: %d\n", i, *ptr); ptr++; } return 0; }
Explanation:
- The pointer ptr starts at arr[0] and increments with each loop iteration to move through the array elements.
Output:
Element 0: 10 Element 1: 20 Element 2: 30 Element 3: 40 Element 4: 50
8. Using Pointers with Multi-dimensional Arrays
When working with multi-dimensional arrays, pointer arithmetic can be used to access elements. For example, a 2D array can be thought of as a series of arrays in contiguous memory.
#include <stdio.h> int main() { int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; int *ptr = &matrix[0][0]; for (int i = 0; i < 6; i++) { printf("Element %d: %d\n", i, *(ptr + i)); } return 0; }
Explanation:
- ptr points to the first element of matrix.
- The loop accesses elements by incrementing ptr, treating the 2D array as a single 1D array.
Output:
Element 0: 1 Element 1: 2 Element 2: 3 Element 3: 4 Element 4: 5 Element 5: 6
9. Character Pointer Arithmetic
Pointer arithmetic works similarly with char arrays (strings), moving one byte at a time.
#include <stdio.h> int main() { char str[] = "Hello"; char *ptr = str; while (*ptr != '\0') { printf("%c ", *ptr); ptr++; } printf("\n"); return 0; }
Explanation:
- ptr points to each character in str and increments until it reaches the null terminator (‘\0’).
Output:
H e l l o
10. Summary Table of Pointer Arithmetic Operations
Operation | Description | Code Example |
---|---|---|
Increment pointer | Moves to next element | ptr++ |
Decrement pointer | Moves to previous element | ptr– |
Add integer to pointer | Moves forward by specified elements | ptr + n |
Subtract integer from pointer | Moves backward by specified elements | ptr – n |
Pointer difference | Gets number of elements between two pointers | diff = ptr2 – ptr1; |
Iterate array with pointer | Loop through array elements using pointer | for (i = 0; i < size; i++) *ptr++ |
2D array traversal | Access 2D array as a 1D array | *(ptr + i) |
String traversal | Iterate through a string | while (*ptr != ‘\0’) { *ptr++; } |
Complete Example: Array Manipulation with Pointer Arithmetic
This example demonstrates using pointer arithmetic to calculate the sum and average of an array.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int size = sizeof(arr) / sizeof(arr[0]); int *ptr = arr; int sum = 0; for (int i = 0; i < size; i++) { sum += *ptr; // Dereference pointer to get value ptr++; // Move to the next element } float average = (float)sum / size; printf("Sum: %d\n", sum); printf("Average: %.2f\n", average); return 0; }
Explanation:
- ptr is initially set to point to arr[0].
- In each iteration, the value at ptr is added to sum, and ptr is incremented to move to the next element.
- After the loop, sum contains the total, and average is calculated.
Output:
Sum: 150 Average: 30.00
Pointer arithmetic provides a powerful way to work with arrays and memory locations in C