Pointer-Arithmetic-in c++ .

In this session i have discussed pointer arithmetic in C++ .


Pointer can perform the arithmetic operation as below .

1-Addition

2-Subtraction .

3-Multiplication .

4-Division .

5-Increment .

6-Decrement .

7-Comparision .


1-Addition

We can perform the addition operation in pointer .

Example : Write a program that demonstrate the addition in pointer .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,sum;
          x=20;
          y=10;
          sum=0;
          ptr1=&x;
          ptr2=&y;
          sum=*ptr1+*ptr2;
          cout<<"Sum : "<<sum;
          return 0;
    }
    
    OUTPUT : Sum : 30
    

2-Subtraction

We can perform the subtraction operation in pointer .

Example : Write a program that demonstrate the addition in pointer .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,sub;
          x=20;
          y=10;
          sub=0;
          ptr1=&x;
          ptr2=&y;
          sub=*ptr1-*ptr2;
          cout<<"Sub : "<<sub;
          return 0;
    }
    
    OUTPUT : Sum : 10
    

3-Multiplication

We can perform the multiplication operation in pointer .

Example : Write a program that demonstrate the addition in pointer .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,mul;
          x=20;
          y=10;
          mul=0;
          ptr1=&x;
          ptr2=&y;
          mul=*ptr1 * *ptr2;
          cout<<"Multi : "<<mul;
          return 0;
    }
    
    OUTPUT : Sum : 200
    

4-Division

We can perform the division operation in pointer .

Example : Write a program that demonstrate the addition in pointer .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,div;
          x=20;
          y=10;
          div=0;
          ptr1=&x;
          ptr2=&y;
          div=*ptr1 / *ptr2;
          cout<<"Div : "<<div;
          return 0;
    }
    
    OUTPUT : Div : 2
    

5-Increment

We can perform the increment operation in pointer . When pointer will increment it will increase the 4 bytes of memory to next location . It is depends on 32 bit or 64 bit operating system .

Example : Write a program that demonstrate the increment in pointer .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int x;
          int*ptr;
          x=20;
          ptr=&x;
          ptr=ptr+1;
          cout<<"\n x address is : "<<&x;
          cout<<"\n after increment address is : "<<ptr;
          return 0;
    }
    
    OUTPUT : x address is :  0x28fef8
             after increment address is : 0x28fefc
    

6-Decrement

We can perform the decrement operation in pointer . When pointer will decrement it will decrease the 4 bytes of memory to before location . It is depends on 32 bit or 64 bit operating system .

Example : Write a program that demonstrate the decrement in pointer .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int x;
          int*ptr;
          x=20;
          ptr=&x;
          ptr=ptr-1;
          cout<<"\n x address is : "<<&x;
          cout<<"\n after decrement address is : "<<ptr;
          return 0;
    }
    
    OUTPUT : x address is : 0x28fef8
             after decrement address is : 0x28fef4
    

7-Comparision

We can perform the comparision operation in pointer . When pointer will decrement it will decrease the 4 bytes of memory to before location . It is depends on 32 bit or 64 bit operating system .

Example : Write a program that demonstrate the decrement in pointer .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int x;
          int*ptr;
          x=20;
          ptr=&x;
          if(*ptr==x)
          {
             cout<<"Same value";
          }
          else
          {
             cout<<"not same value";
          }
          return 0;
    }
    
    OUTPUT : Same value    

Welcome to AndroidATPoint.

back image                     next image