Pointer Arithmetic in C Programming
Pointer Arithmetic in C Programming
Introduction to Pointer Arithmetic in C Programming
Pointer arithmetic in C programming allows you to perform operations on pointers. These operations help in navigating through memory efficiently, especially when working with arrays and dynamic data.
Understanding pointer arithmetic in C programming is important for advanced programming and memory handling.
What is Pointer Arithmetic
Pointer arithmetic refers to operations performed on pointer variables such as increment, decrement, addition, and subtraction.
These operations move the pointer to different memory locations based on the data type.
Types of Pointer Arithmetic Operations
Increment Operator (++)
Moves the pointer to the next memory location.
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
printf(“%d\n”, *ptr); // 10
ptr++;
printf(“%d\n”, *ptr); // 20
return 0;
}
Decrement Operator (–)
Moves the pointer to the previous memory location.
int main() {
int arr[3] = {10, 20, 30};
int *ptr = &arr[2];
printf(“%d\n”, *ptr); // 30
ptr—;
printf(“%d\n”, *ptr); // 20
return 0;
}
Addition of Integer to Pointer
You can add an integer to a pointer to move forward multiple positions.
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
ptr = ptr + 2;
printf(“%d”, *ptr); // 3
return 0;
}
Subtraction of Integer from Pointer
You can subtract an integer to move backward.
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[4];
ptr = ptr – 2;
printf(“%d”, *ptr); // 3
return 0;
}
Difference Between Two Pointers
You can subtract two pointers to find the number of elements between them.
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr1 = &arr[4];
int *ptr2 = &arr[1];
printf(“Difference = %ld”, ptr1 – ptr2); // Output: 3
return 0;
}
Pointer Arithmetic with Arrays
Pointers and arrays are closely related. Pointer arithmetic is often used to traverse arrays.
int main() {
int arr[4] = {5, 10, 15, 20};
int *ptr = arr;
int i;
for(i = 0; i < 4; i++) {
printf(“%d\n”, *(ptr + i));
}
return 0;
}
Important Rules of Pointer Arithmetic
Key Rules
- Pointer arithmetic depends on data type size
- Only addition and subtraction are allowed
- Cannot multiply or divide pointers
- Pointers must belong to same array for subtraction
Why Pointer Arithmetic is Important
Key Benefits
- Efficient array traversal
- Faster memory access
- Useful in dynamic memory allocation
- Essential for advanced C programming
Start Learning C Programming
Practice pointer arithmetic to understand memory navigation in C programming.
Summary
Pointer arithmetic in C programming allows movement across memory locations. It is useful for arrays, memory management, and efficient coding.
FAQs
What is pointer arithmetic in C programming?
It is performing operations like increment and addition on pointers.
Can we add two pointers?
No, only subtraction is allowed.
What does ptr++ do?
It moves the pointer to the next memory location.
Why is pointer arithmetic important?
It helps in efficient memory handling and array traversal.



