You can execute arithmetic operations on a pointer just like you can on a numeric value because, as you now know, a pointer is an address, which is a kind of numeric value. The ++, --, +, and -are the main four types of arithmetic operators which can be applied to the pointers.
Let's assume that ptr is an integer pointer that points to the address 1000 in order to understand pointer arithmetic more clearly. Let's do the following arithmetic operation on the pointer, assuming that we are working with 32-bit integers:
ptr++
Because the ptr will always point to the next integer when it is increased, it will always point to the location 1004 in this case. Without changing the actual value at the memory address, this operation will shift the pointer to the following memory location. The above operation will point to the location 1001 if ptr points to a character whose address is 1000 since the following character will then be available at 1001.
In our program, we prefer to use a pointer rather than an array because a pointer may be incremented whereas an array name cannot because it is a constant pointer.
The program which is given below increases the variable pointer in order to access each succeeding element within the array.
#include <iostream>
using namespace std;
const int MAX = 4;
int main () {
int var[MAX] = {20, 200, 400,800};
int *ptr;
// let us have an array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the very next location
ptr++;
}
return 0;
}
Output:
Address of var[0] = 0x7ffd1e87ae70 Value of var[0] = 20 Address of var[1] = 0x7ffd1e87ae74 Value of var[1] = 200 Address of var[2] = 0x7ffd1e87ae78 Value of var[2] = 400 Address of var[3] = 0x7ffd1e87ae7c Value of var[3] = 800
The same rules will be applied when decrementing a pointer, which reduces its value by the number of bytes appropriate to its data type as illustrated below.
#include <iostream>
using namespace std;
const int MAX = 4;
int main () {
int var[MAX] = {20, 200, 400,800};
int *ptr;
// let us have the address of the very last element in pointer.
ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the previous location
ptr--;
}
return 0;
}
Output:
Address of var[4] = 0x7ffc911ead8c Value of var[4] = 800 Address of var[3] = 0x7ffc911ead88 Value of var[3] = 400 Address of var[2] = 0x7ffc911ead84 Value of var[2] = 200 Address of var[1] = 0x7ffc911ead80 Value of var[1] = 20
Comparing pointers is possible using relational operators like ==,, and >. P1 and P2 can be meaningfully compared if they both point to variables that are connected to one another, such as the items of the same array.
The program that comes next modifies the preceding example by incrementing the variable pointer. The address to which it points to must be either more than or equal to the address of the array's final element, which really is &var[MAX - 1].
#include <iostream>
using namespace std;
const int MAX = 4;
int main () {
int var[MAX] = {20, 200, 400,800};
int *ptr;
// let us have an address of the first element in pointer.
ptr = var;
int i = 0;
while ( ptr <= &var[MAX - 1] ) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the previous location
ptr++;
i++;
}
return 0;
}
Output:
Address of var[0] = 0x7ffc438558c0 Value of var[0] = 20 Address of var[1] = 0x7ffc438558c4 Value of var[1] = 200 Address of var[2] = 0x7ffc438558c8 Value of var[2] = 400 Address of var[3] = 0x7ffc438558cc Value of var[3] = 800