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 .

    #includ<stdio.h> 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,sum;
          x=20;
          y=10;
          sum=0;
          ptr1=&x;
          ptr2=&y;
          sum=*ptr1+*ptr2;
          printf("Sum : %d",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 .

    #includ<stdio.h> 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,sub;
          x=20;
          y=10;
          sub=0;
          ptr1=&x;
          ptr2=&y;
          sub=*ptr1-*ptr2;
          printf("Sub : %d",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 .

    #includ<stdio.h> 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,mul;
          x=20;
          y=10;
          mul=0;
          ptr1=&x;
          ptr2=&y;
          mul=*ptr1 * *ptr2;
          printf("Multi : %d",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 .

    #includ<stdio.h> 
    int main()
    {  
          int *ptr1,*ptr2;
          int x,y,div;
          x=20;
          y=10;
          div=0;
          ptr1=&x;
          ptr2=&y;
          div=*ptr1 / *ptr2;
          printf("Div : %d",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 .

    #includ<stdio.h> 
    int main()
    {  
          int x;
          int*ptr;
          x=20;
          ptr=&x;
          ptr=ptr+1;
          printf("x address is : %u",&x);
          printf("after increment address is : %u",ptr);
          return 0;
    }
    
    OUTPUT : x address is : 2686728
             after increment address is : 2686732
    

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 .

    #includ<stdio.h> 
    int main()
    {  
          int x;
          int*ptr;
          x=20;
          ptr=&x;
          ptr=ptr-1;
          printf("x address is : %u",&x);
          printf("after decrement address is : %u",ptr);
          return 0;
    }
    
    OUTPUT : x address is : 2686728
             after decrement address is : 2686724
    

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 .

    #includ<stdio.h> 
    int main()
    {  
          int x;
          int*ptr;
          x=20;
          ptr=&x;
          if(*ptr==x)
          {
             printf("Same value");
          }
          else
          {
             printf("not same value");
          }
          return 0;
    }
    
    OUTPUT : Same value    

Welcome to AndroidATPoint.

back image                     next image