Topic :Pointers Arithmetic in C++: Incrementing | Decrementing | Comparisons



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:


  • » Increment Operator ( ++ )
    » Decrements Operator ( -- )
    » Addition ( + )
    » Subtraction ( - )



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;
}


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;
}


Pointer Comparisons:

Pointers May be compared using the Relational Operators, such as

  • » Equal to (==).
    » Greater than (>).
    » Less than (<).

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;
}


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.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement