Pointer Arithmetic in C
In the previous lesson, you learned that pointers store memory addresses.
In this lesson, we will learn how pointers can be incremented, decremented, and moved to access different memory locations.
This concept is called pointer arithmetic.
What Is Pointer Arithmetic?
Pointer arithmetic means performing arithmetic operations on pointers instead of normal variables.
But pointer arithmetic is different from normal arithmetic. It depends on the data type of the pointer.
Important Rule to Remember
When you increment a pointer, it does NOT increase by 1 byte.
Instead, it increases by:
Size of the data type it points to
This rule is the heart of pointer arithmetic.
Example: Pointer Arithmetic with int
Assume:
- Size of
int= 4 bytes
If a pointer points to an integer and we increment it, it moves forward by 4 bytes.
Simple Pointer Increment Example
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("Address before increment: %p\n", ptr);
ptr++;
printf("Address after increment: %p\n", ptr);
return 0;
}
Here:
ptr++moves the pointer to the next integer location- The address changes by 4 bytes (on most systems)
Pointer Arithmetic with Arrays
Arrays and pointers are closely related.
The name of an array itself acts like a pointer to the first element.
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
Here:
ptrpoints toarr[0]ptr + 1points toarr[1]
Accessing Array Elements Using Pointer Arithmetic
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
int i;
for (i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
This program prints array elements using pointer arithmetic instead of array indexing.
Pointer Decrement
Pointers can also be decremented.
ptr--;
This moves the pointer backward by the size of the data type.
What Operations Are Allowed on Pointers?
- Increment (
ptr++) - Decrement (
ptr--) - Add integer (
ptr + n) - Subtract integer (
ptr - n)
Invalid operations:
- Adding two pointers
- Multiplying pointers
- Dividing pointers
Real-World Thinking
Think of memory like apartments in a building.
- Each apartment has the same size
- Pointer moves apartment by apartment
- Size of apartment depends on data type
Pointer arithmetic helps navigate memory correctly.
Common Mistakes
- Moving pointer outside array bounds
- Dereferencing invalid memory
- Confusing pointer arithmetic with integer arithmetic
These mistakes may cause crashes or undefined behavior.
Mini Practice
- Access array elements using pointer arithmetic
- Print addresses of array elements
- Use pointer increment to traverse an array
Quick Quiz
Q1. What happens when a pointer is incremented?
It moves to the next memory location based on data type size.
Q2. What does ptr + 1 point to?
The next element of the same data type.
Q3. Can we add two pointers?
No, adding two pointers is not allowed.
Q4. What does *(ptr + i) mean?
It accesses the value at index i using pointer arithmetic.
Q5. Why is pointer arithmetic useful?
It allows efficient traversal of arrays and memory.