POINTER ARITHMETIC:


Pointers, as understood, represent addresses in memory and are treated as numeric values, allowing us to perform arithmetic operations on them just like we would with regular numbers. The fundamental arithmetic operators applicable to pointers are:

  • Increment Operator (++):Increases the pointer value.
  • Decrement Operator (--):Decreases the pointer value.
  • Addition (+):Adds a numeric value to the pointer.
  • Subtraction (-):Subtracts a numeric value from the pointer.

To grasp pointer arithmetic, let's consider an example where Ptris an integer pointer with a current value of 250, meaning Ptrpoints to the address 250 in memory, assuming 32-bit integers. We'll perform the following arithmetic operations:

Ptr++;

After this increment operation, Ptr's contents will become 254, not 251. This behavior is due to each increment shifting the pointer to the next integer's memory location. Similarly, decrements follow the same logic. For instance:

Ptr--;

Following this decrement, Ptr's contents will be reduced to 246.

Pointers offer advantages over arraysbecause they allow for incrementing. Unlike array names, which remain constant pointers, each pointer increment directs it to the next element of its base type. Consider the following example:.

C++ Copy to Clipboard  
#include<iostream> 
using namespace std;
main()
{
    int var[5] = {10,20,30,40,50}; // Array Declaration.
    int *ptr; // Pointer pointing to int.
 
    ptr = var; // Storing array address in pointer.
 
    for (int i = 0; i < 5; i++) // Loop to display Address and Value:
    {
       cout << "\n Address of var[" << i << "] = " << ptr << endl; // Display the Address.
       cout << "Value of var[" << i << "] = " << *ptr << endl; // Display the value.
 
       ptr++; // Move to the next location (incrementation).
    }
 
    return 0;
}

Decrementing a pointer operates similarly, reducing its value by the number of bytes of its data type. With each decrement, the pointer shifts to the previous element of its base type. Here's an illustrative example:

C++ Copy to Clipboard  
#include<iostream> 
using namespace std;
main()
{
    int var[5] = {10,20,30,40,50}; // Array Declaration.
    int *ptr; // Pointer pointing to int.
 
    ptr = &var[4]; // Pointer points to the last address of Array.
 
    for (int i = 5; i > 0; i--) // Loop to display Address and Value:
    {
       cout << "\n Address of var[" << i << "] = " << ptr << endl; // Display the Address.
       cout << "Value of var[" << i << "] = " << *ptr << endl; // Display the value.
 
       ptr--; // Move to the previous location.
    }
 
    return 0;
}

Pointers can be compared using relational operators like Equal to ==, Greater than , and Less than <. For meaningful comparison outcomes, the pointers should relate to each other, such as pointing to elements within the same array. Null pointer comparison, where a pointer is compared to the null pointer (zero), is another valid comparison.

C++ Copy to Clipboard  
#include<iostream> 
using namespace std;
main()
{
   int var[5] = {10,20,30,40,50}; // Array Declaration.
   int *ptr; // Pointer pointing to int.
 
   ptr = var; // Pointer points to the first address of Array.
   int i=0; // Variable for the while loop.
 
   while(ptr <= &var[4]) // Loop to display Address and Value:
   {
      for(int i=0; i<5; i++)
      {
         cout << "\n Address of var[" << i << "] = " << ptr << endl; // Display the Address.
         cout << "Value of var[" << i << "] = " << *ptr << endl; // Display the value.
 
         ptr++; // Move to the next location.
       }
    }
 
    return 0;
}