C - Pointer Arithmetic: Mastering Pointer Operations
Explore the fundamentals of pointer arithmetic in C programming. This chapter details how to perform arithmetic operations on pointers, including incrementing, decrementing, and comparing pointers, enhancing your understanding of memory management and data manipulation.
C - Pointer Arithmetic
A pointer variable in C stores the address of another variable, and since addresses are integers, you can perform arithmetic operations on pointers. This chapter covers the arithmetic operators that can be used with pointers in C and explains the operations in detail.
Overview of Pointer Arithmetic Operations
- Increment and Decrement of a Pointer
- Addition and Subtraction of Integer to Pointer
- Subtraction of Pointers
- Comparison of Pointers
Increment and Decrement of a Pointer
The ++
and --
operators are used for incrementing and decrementing values in C. When applied to a pointer, these operators change the pointer's address based on the size of the data type it points to.
For example, consider an integer variable x
stored at address 1000:
int x = 10; // created at address 1000
x++; // x becomes 11
If y
is a pointer to x
, and we increment y
:
int *y = &x; // y points to x (address 1000)
y++; // y becomes 1004 (increments by size of int)
Example of Incrementing a Pointer
#include
int main() {
int x = 10;
int *y = &x;
printf("Value of y before increment: %d\n", y);
y++;
printf("Value of y after increment: %d", y);
}
Output:
Value of y before increment: 6422036 Value of y after increment: 6422040
Example of Decrementing a Pointer
#include
int main() {
double x = 10;
double *y = &x;
printf("Value of y before decrement: %ld\n", y);
y--;
printf("Value of y after decrement: %ld", y);
}
Output:
Value of y before decrement: 6422032 Value of y after decrement: 6422024
Traversing an Array by Incrementing Pointer
When an array is declared, the elements are stored in adjacent memory locations. The following example demonstrates how to traverse an array using pointer increment:
#include
int main() {
int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int len = sizeof(a) / sizeof(int);
int *x = a;
int i;
for (i = 0; i < len; i++) {
printf("Address of subscript %d = %d Value = %d\n", i, x, *x);
x++;
}
return 0;
}
Output:
Address of subscript 0 = 6421984 Value = 10 Address of subscript 1 = 6421988 Value = 20 Address of subscript 2 = 6421992 Value = 30 Address of subscript 3 = 6421996 Value = 40 Address of subscript 4 = 6422000 Value = 50 Address of subscript 5 = 6422004 Value = 60 Address of subscript 6 = 6422008 Value = 70 Address of subscript 7 = 6422012 Value = 80 Address of subscript 8 = 6422016 Value = 90 Address of subscript 9 = 6422020 Value = 100
Addition and Subtraction of Integer to Pointer
An integer can be added to or subtracted from a pointer, moving the pointer to the next or previous memory address based on the size of the data type.
Example of Adding Value to a Pointer
#include
int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = int_arr;
printf("Value at ptrArr: %d\n", *ptrArr);
ptrArr = ptrArr + 2; // Moving to third element
printf("Value at ptrArr after adding 2: %d\n", *ptrArr);
return 0;
}
Output:
Value at ptrArr: 12 Value at ptrArr after adding 2: 45
Example of Subtracting Value from a Pointer
#include
int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = &int_arr[4]; // points to last element
printf("Value at ptrArr: %d\n", *ptrArr);
ptrArr = ptrArr - 2; // Moving to third element
printf("Value at ptrArr after subtracting 2: %d\n", *ptrArr);
return 0;
}
Output:
Value at ptrArr: 89 Value at ptrArr after subtracting 2: 45
Subtraction of Pointers
Subtraction of two pointers is meaningful in C. It returns the number of data types that can fit between the two pointers.
Example of Subtracting Two Pointers
#include
int main() {
int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int *x = &a[0]; // zeroth element
int *y = &a[9]; // last element
printf("Address of a[0]: %ld Address of a[9]: %ld\n", x, y);
printf("Subtraction of two pointers: %ld", y - x);
return 0;
}
Output:
Address of a[0]: 140729162482768 Address of a[9]: 140729162482804 Subtraction of two pointers: 9
Comparison of Pointers
Pointers can be compared using relational operators. In the example below, we compare two pointers that point to the first and last elements of an array.
Example of Comparing Pointers
#include
const int MAX = 3;
int main() {
int var[] = {10, 100, 200};
int i, *ptr1, *ptr2;
// Initializing pointers
ptr1 = var;
ptr2 = &var[MAX - 1];
while (ptr1 <= ptr2) {
printf("Address of var[%d] = %p\n", i, ptr1);
printf("Value of var[%d] = %d\n", i, *ptr1);
ptr1++;
i++;
}
return 0;
}
Output:
Address of var[0] = 0x7ffe7101498c Value of var[0] = 10 Address of var[1] = 0x7ffe71014990 Value of var[1] = 100 Address of var[2] = 0x7ffe71014994 Value of var[2] = 200