Pointer Arithmetic:
As we Understood Pointer is an Address which is a Numeric value, therefore, we can perform Arithmetic Operation on a pointer just as we can a Numeric value. There are Only four Arithmetic Operators:
To Understand what occurs in pointer Arithmetic, let Ptr be an int pointer with a current value of 250, (i.e. Ptr points to the address 250) . Assuming 32-bit integers, Let's Perform the following Arithmetic Operation on the pointer:
ptr++;
After the Increment, The contents of Ptr will be 254, not 251. The Reason for this is that each time Ptr is incremented, it will point to the Next int. The same is true of decrements. For Example: Again Assuming that Ptr has the value 250. so let's Perform the Following Arithmetic Operation on the pointer.
ptr--;
After the Decrements, The contents of Ptr will be 246.
“
When a Pointer is used to hold the Memory Address of a Simple variable, do not increment or decrements the pointer. When a pointer is used to hold the address of an Array, it Makes sense to increment or decrements the pointer.
Incrementing a Pointer:
We prefer using a pointer in our program instead of an Array because the variable pointer can be incremented, Unlike the Array Name which cannot be incremented because it is a constant pointer. Each time that a pointer is incremented, it will point to the memory location of the Next element of its base type.
Let's have an example, in this example our program will increments the variable pointer to access each succeeding element of the Array:
/* Incrementing a Pointer : InfoBrother*/
#include<iostream>
using namespace std;
main()
{
int var[5] = {10,20,30,40,50}; //Array Declaration.
int *ptr; //pointer point to int.
ptr = var; // let us have array address in pointer.
for (int i = 0; i < 5; i++) //Loop to show Address and Value:
{
cout << "\n Address of var[" << i << "] = " <<ptr<<endl; //show the Address:
cout << "Value of var[" << i << "] = "<<*ptr<<endl; //show the value:
ptr++; // point to the next location (incrementation)
}
return 0;
}
Incrementing a Pointer
Address of var[0] = 0xdffd60
Value of var[0] = 10
Address of var[1] = 0xdffd64
Value of var[1] = 20
Address of var[2] = 0xdffd68
Value of var[2] = 30
Address of var[3] = 0xdffd6c
Value of var[3] = 40
Address of var[4] = 0xdffd70
Value of var[4] = 50
Decrementing a Pointer:
Same goes for Decrementing a Pointer, which decreases its value by the Number of bytes of its data type. Each time it is decremented, it will point to the location of the previous element of its base type.
Let's have an example, in this example our program will Decrements the variable pointer to access each succeeding element of the Array:
/* Decrementing a Pointer : InfoBrother*/
#include<iostream>
using namespace std;
main()
{
int var[5] = {10,20,30,40,50}; //Array Declaration.
int *ptr; //pointer point to int.
ptr = &var[4]; //ptr point to the last address of Array:
for (int i = 5; i > 0; i--) //Loop to show Address and Value:
{
cout << "\n Address of var[" << i << "] = " <<ptr<<endl; //show the Address:
cout << "Value of var[" << i << "] = "<<*ptr<<endl; //show the value:
ptr--; // point to the Previous location:
}
return 0;
}
Decrementing a Pointer:
Address of var[5] = 0xdffd70
Value of var[5] = 50
Address of var[4] = 0xdffd6c
Value of var[4] = 40
Address of var[3] = 0xdffd68
Value of var[3] = 30
Address of var[2] = 0xdffd64
Value of var[2] = 20
Address of var[1] = 0xdffd60
Value of var[1] = 10
Pointer Comparisons:
Pointers May be compared using the Relational Operators, such as
In general, for the outcome of a pointer comparison to be meaningful, the two pointers must have some relationship to each other. For Example: both may point to elements within the same Arrays. One other type of pointer comparison is to compared the pointer to the Null Pointer, which is zero.
If ptr1 and ptr2 point to variables that are related to each other, such as elements of the same Array, Then ptr1 and ptr2 can be meaningfully compared.
The following program modifies the previous example one by incrementing the variable pointer so long as the address to which it points is either less than or equal to the address of the last element of the Array, which is &var[4];
/* Pointer Comparisons : InfoBrother*/
#include<iostream>
using namespace std;
main()
{
int var[5] = {10,20,30,40,50}; //Array Declaration.
int *ptr; //pointer point to int.
ptr = var; //ptr point to the first address of Array:
int i=0; //Variable to use in while loop:
while(ptr <= &var[4]) //Loop to show Address and Value:
{
for(int i=0; i<5; i++)
{
cout << "\n Address of var[" << i << "] = "
<<ptr<<endl; //show the Address:
cout << "Value of var[" << i << "] = "
<<*ptr<<endl; //show the value:
ptr++; // point to the Next location:
}
}
return 0;
}
Pointer Comparisons:
Address of var[0] = 0xdffd60
Value of var[0] = 10
Address of var[1] = 0xdffd64
Value of var[1] = 20
Address of var[2] = 0xdffd68
Value of var[2] = 30
Address of var[3] = 0xdffd6c
Value of var[3] = 40
Address of var[4] = 0xdffd70
Value of var[4] = 50
“
We are not limited to only increment and decrements Operations. We can also add or subtract integers to or from pointers.
Like ptr = ptr1 + 5; Makes Ptr1 point to the ninth element of ptr1's base type, beyond the one to which it is currently pointing.