Pointer Arithmetic in C Language
0 327
In C, pointer arithmetic allows you to work with memory locations (addresses) using pointers.
Pointers are variables that store the memory location (address) of another variable.
With pointer arithmetic, you can quickly and easily access and modify data stored in memory by adding or subtracting numbers from a pointer.
In C, a pointer is a variable that accesses the memory location(address) of another variable.
This makes the pointer to point different memory locations.
Types of operations performed in Pointer arithmetic
1Increment operation
2Decrement operation
3Addition operation
4Subtraction operation
5Pointer Comparison
Increment operation
We have a pointer pointing to a specific memory address.
When we increment that pointer to handle the data in memory.
pointer arithmetic in C allows you to move to the pointer memory location by adding or subtracting numbers from it.
This helps you access and change data stored in memory more conveniently.
This is done by adding value (size of datatype being pointed) to the pointer's current value.
Example:
// Program of Increment OperationOutput:
#include <stdio.h>int main() {
int A[4] = {15, 25, 35, 45};
int *ptr = A; // Pointer to the first element of the array
printf("Initial pointer value: %p\n", *ptr);
ptr = ptr+1;
printf("After incrementing, pointer value: %p\n", *ptr); // Print incremented pointer value
return 0;
}
Initial pointer value: 0xf
After incrementing, pointer value: 0x19
Decrement operation
We have a pointer pointing to a specific memory address.
When we decrement that pointer to handle the data in memory.
pointer arithmetic in C allows you to move the pointer to the previous memory location by subtracting a number from it.
This enables you to access data stored earlier in memory.
This is done by subtracting the value (size of the datatype being pointed) from the pointer's current value.
Example:
// Program for decrement OperationOutput:
#include <stdio.h>
int main() {
int A[4] = {15, 25, 35, 45};
int *ptr = &A[3]; // Pointer to the first element of the array
printf("Initial pointer value: %p\n", *ptr);
ptr = ptr-1;
printf("After decrement, pointer value: %p\n", *ptr);
return 0;
}
Initial pointer value: 0x2d
After decrement, pointer value: 0x23
Addition operation
Pointer addition is shifting pointer to take a step forward by a certain number of memory cells.
When you add a number to a pointer in C, it moves forward in memory by that number of cells.
It's like instructing the pointer to jump ahead by a certain number of steps. Conversely, if you subtract a number from the pointer, it moves backward in memory, similar to taking steps back.
This way, pointer arithmetic allows you to navigate to different spots in memory easily.
Example:
// Program for Addition operation
#include <stdio.h>
int main() {
int A[4] = {15, 25, 35, 45};
int *ptr = A; // Pointer to the first element of the array
printf("Initial pointer value: %p\n", *ptr);
ptr = ptr+2;
printf("After adding, pointer value: %p\n", *ptr);
return 0;}
Output:
Initial pointer value: 0xf
After adding, pointer value: 0x23
Subtraction operation
Subtraction operation is shifting the pointer to take a step backward by a certain number of memory cells.
This makes the pointer move to a different spot in the memory.
If you subtract a number from the pointer it moves backward by that number of memory cells.
It's similar to telling the pointer to jump ahead or behind by a specified number of steps.
Example:
// Program for Subtraction operationOutput:
#include<stdio.h>
int main() {
int A[4] = {15, 25, 35, 45};
int *ptr = &A[3]; // Pointer to the first element of the array
printf("Initial pointer value: %p\n", *ptr);
ptr = ptr-2;
printf("After adding, pointer value: %p\n", *ptr);
return 0;
}
pointer value: 0x2d
After adding, pointer value: 0x19
Pointer comparison
Pointer comparison involves determining a relationship between two pointers.
These operations help you move around in memory, change data, and make decisions based on where things are stored.
Example:
// Program for Pointer comparisonOutput:
#include<stdio.h>
int main() {
int A[4] = {10, 20, 30, 40};
int *ptr1 = &A[2];
int *ptr2 = &A[4];
if (ptr1 == ptr2) {
printf("Pointers point to the same location.\n");
} else if (ptr1 < ptr2) {
printf("ptr1 points to a memory location before ptr2.\n");
} else {
printf("ptr1 points to a memory location after ptr2.\n");
}
return 0;
}
ptr1 points to a memory location before ptr2.
Share:
Comments
Waiting for your comments