Pointer Arithmetic in C
Pointer Arithmetic refers to performing mathematical operations on pointers.
Since pointers represent memory addresses, increasing or decreasing them moves the pointer by the size of its data type.
Why Pointer Arithmetic?
Pointer arithmetic allows you to:
- Traverse arrays using pointers
- Access memory sequentially
- Manipulate data stored in structures or strings
- Perform efficient low-level operations
Valid Pointer Arithmetic Operations
You can perform:
- Pointer + Integer
- Pointer - Integer
- Pointer++ / Pointer--
- Pointer1 - Pointer2 (same array only)
You CANNOT perform:
- Pointer + Pointer ❌
- Pointer * Pointer ❌
- Pointer / Pointer ❌
How Increment Works?
When a pointer increments, it does not increase by 1 byte, it increases by the size of its data type.
- int pointer → +4 bytes (on most systems)
- float pointer → +4 bytes
- char pointer → +1 byte
- double pointer → +8 bytes
Example: Pointer Increment
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("%p\n", p);
p++;
printf("%p\n", p);
return 0;
}
This moves the pointer ahead by 4 bytes (size of int).
Pointer and Arrays
- Pointers naturally move across array elements
- Array name itself is a pointer constant
#include <stdio.h>
int main() {
int a[3] = {10, 20, 30};
int *p = a;
printf("%d\n", *p);
p++;
printf("%d\n", *p);
return 0;
}
Pointer moves across array indexes: a[0] → a[1] → a[2]
Pointer Subtraction
Two pointers can be subtracted only if they belong to the same array.
int a[5] = {1,2,3,4,5};
int *p = &a[1];
int *q = &a[4];
printf("%ld", q - p); // Output: 3
Pointer Comparison
- Useful in loops
- Allowed only within the same array
while(p < q){
p++;
}
Pointer Arithmetic Rules
- Pointers must point to valid memory
- Arithmetic moves by type size
- Pointer subtraction gives distance
- Pointer comparison works only for related pointers
Real-Life Uses
- Array processing
- String traversal
- Dynamic memory block manipulation
- Low-level hardware access
Pointer Arithmetic – 10 Examples
1. Incrementing a Pointer
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("Address before: %p\n", p);
p++;
printf("Address after: %p", p);
return 0;
}
2. Pointer Increment on Array
#include <stdio.h>
int main() {
int arr[] = {10,20,30};
int *p = arr;
printf("%d\n", *p);
p++;
printf("%d\n", *p);
return 0;
}
3. Decrementing a Pointer
#include <stdio.h>
int main() {
int arr[] = {5,10,15};
int *p = &arr[2];
printf("%d\n", *p);
p--;
printf("%d", *p);
return 0;
}
4. Adding an Integer to Pointer
#include <stdio.h>
int main() {
int arr[] = {2,4,6,8,10};
int *p = arr;
p = p + 3;
printf("%d", *p);
return 0;
}
5. Subtracting an Integer from Pointer
#include <stdio.h>
int main(){
int arr[] = {100,200,300,400};
int *p = &arr[3];
p = p - 2;
printf("%d", *p);
return 0;
}
6. Difference Between Two Pointers
#include <stdio.h>
int main(){
int arr[] = {1,2,3,4,5};
int *p = &arr[4];
int *q = &arr[1];
printf("Difference = %ld", p - q);
return 0;
}
7. Pointer Comparison
#include <stdio.h>
int main(){
int arr[] = {10,20,30};
int *p = &arr[0];
int *q = &arr[2];
if(p < q)
printf("p is before q");
else
printf("p is after q");
return 0;
}
8. Accessing Array Using Pointer
#include <stdio.h>
int main(){
int arr[] = {4,8,12};
int *p = arr;
for(int i=0; i<3; i++){
printf("%d ", *(p + i));
}
return 0;
}
9. Pointer to Float – Arithmetic
#include <stdio.h>
int main(){
float arr[] = {1.1, 2.2, 3.3};
float *p = arr;
p++;
printf("%.1f", *p);
return 0;
}
10. Pointer Arithmetic with Characters
#include <stdio.h>
int main(){
char str[] = "ABCDEF";
char *p = str;
p = p + 4;
printf("%c", *p);
return 0;
}
Practice Questions
- What happens when you increment an int pointer?
- Why can pointer subtraction be done only within arrays?
- Explain pointer comparison with an example.
- How does pointer arithmetic depend on data type size?
- What is the difference between p++ and ++p for pointers?
Practice Task:
Create an array of 5 integers and traverse all elements using ONLY pointer arithmetic (no loops with indexes).