Pointer Arithmetic
Pointer arithmetic is one of the most powerful features of C programming! It allows you to perform mathematical operations on pointers to navigate through memory efficiently. Think of it like having a GPS for memory addresses - you can move forward, backward, and calculate distances between locations.
What is Pointer Arithmetic?
Pointer arithmetic involves performing mathematical operations on pointer variables. Unlike regular arithmetic, when you add or subtract from a pointer, the compiler automatically scales the operation by the size of the data type the pointer points to.
Basic Operations
1. Increment and Decrement
You can move a pointer to the next or previous memory location:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Points to first element
printf("Current value: %d\n", *ptr); // 10
ptr++; // Move to next element
printf("After increment: %d\n", *ptr); // 20
ptr--; // Move back to previous element
printf("After decrement: %d\n", *ptr); // 10
return 0;
}
2. Addition and Subtraction
You can add or subtract integers from pointers:
#include <stdio.h>
int main() {
int numbers[] = {100, 200, 300, 400, 500};
int *ptr = numbers;
printf("ptr points to: %d\n", *ptr); // 100
printf("ptr + 2 points to: %d\n", *(ptr + 2)); // 300
printf("ptr + 4 points to: %d\n", *(ptr + 4)); // 500
return 0;
}
3. Pointer Subtraction
You can subtract one pointer from another to find the distance between them:
#include <stdio.h>
int main() {
int data[] = {1, 2, 3, 4, 5, 6};
int *start = &data[1]; // Points to element at index 1
int *end = &data[4]; // Points to element at index 4
int distance = end - start; // Number of elements between them
printf("Distance: %d elements\n", distance); // 3
return 0;
}
Important Rules
| Operation | Description | Example |
|---|---|---|
ptr++ | Move to next element | ptr = ptr + 1 |
ptr-- | Move to previous element | ptr = ptr - 1 |
ptr + n | Move n elements forward | Points to ptr[n] |
ptr - n | Move n elements backward | Points to ptr[-n] |
ptr2 - ptr1 | Distance between pointers | Number of elements |
Memory Scaling
Here's the cool part - C automatically handles the size scaling for you:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30};
char str[] = "ABC";
int *int_ptr = arr;
char *char_ptr = str;
printf("int_ptr address: %p\n", int_ptr);
printf("int_ptr + 1 address: %p\n", int_ptr + 1); // Adds 4 bytes (size of int)
printf("char_ptr address: %p\n", char_ptr);
printf("char_ptr + 1 address: %p\n", char_ptr + 1); // Adds 1 byte (size of char)
return 0;
}
Practical Example: Array Traversal
Pointer arithmetic makes array traversal super efficient:
#include <stdio.h>
int main() {
int scores[] = {85, 92, 78, 96, 88};
int size = sizeof(scores) / sizeof(scores[0]);
int *ptr = scores;
printf("Scores using pointer arithmetic:\n");
for (int i = 0; i < size; i++) {
printf("Score %d: %d\n", i + 1, *(ptr + i));
}
return 0;
}
Safety Tips
⚠️ Be Careful! Pointer arithmetic can be dangerous if not used properly:
- Always ensure your pointer stays within valid memory bounds
- Don't subtract pointers that don't point to the same array
- Remember that going out of bounds leads to undefined behavior
Think of pointer arithmetic like walking on a tightrope - it's powerful and efficient, but you need to stay balanced and within safe limits!