Pointers Arithmetic

  • Increment
  • Decrement

Note : We cannot add,multiply,divide,subtract Two pointers.
but, We can add and subtract a number from an Pointer.




Increment(++)




  • Increment operator increments the pointer variable by 1 which gives the location of the next block of same datatype.

  • Incrementing an pointer:
    #include<stdio.h>
    
    void main()
    {
        int a = 10; //integer variable
    
        int *p; //Pointer of level one
    
        p = &a; //assigning the address of variable a
                //to a pointer p of level one
    
        printf("Before incrementing  p: %u",p);
    
    
     p++; //incrementing the pointer
    
     printf("\nAfter Incrementing  p:  %u",p);
    }
    

    Output
    Before incrementing p: 6356744

    After Incrementing p: 6356748




  • By looking the above Output you will say that pointer has incremented by 4,Yes you are technically correct but ,When we increment the pointer it gives the next location of same type.
  • As p is integer pointer as address of p = 6356744 and then we have incremented p and it gave address of the next block which is 6356748
  • Let's see the formula,how pointer increments.

  • Formula:
    ptr = ptr + sizeof(datatype_pointer)
    

    This is what happen when we incremented the Pointer ptr:
    ptr = 6356744 + sizeof(int)
    
    ptr = 6356744 + 4
    
    ptr = 6356748 
    

    • The concept of pointer arithmetic remains exact same, but the size of pointer and various datatypes is different in a different bits of machine.
    • But for now the Size of integer pointer is 4 because the c language which we are studying in that size of integer variable is 4 bytes
    • So, to know what is the size of data types in your machine than you can use sizeof() function which returns the size of datatypes

    In above case pointer was of integer data type,but what if it was float or char type.

    • Nothing will change just it will increment the pointer by the respected size of the data type
    • If pointer is of char type suppose address of char pointer p is 5000 than if we increment it then it will give p = 5001
    • Similarly , if Pointer is of Float type , suppose p = 5000 than if we increment the pointer than it give p = 5004




    Incrementing the float Pointer:
    #include<stdio.h>
    void main()
    {
        float a = 10.1; //float variable
    
        float *p; //Pointer of level one
    
        p = &a //assigning the address of variable a
                //to a pointer p of level one
    
        printf("Before incrementing  p: %u",p);
    
    
    
     p++; //incrementing the pointer
    
     printf("\nAfter Incrementing  p:  %u",p);
    }
    

    Output
    Before incrementing p: 6356744

    After Incrementing p: 6356748

    This is what happen when we incremented the float Pointer p:
    p = 6356744 + sizeof(float)
    
    ptr = 6356744 + 4
    
    ptr = 6356748 
    





    Incrementing the Char Pointer:
    #include<stdio.h>
    void main()
    {
        char a = 'A'; //char variable
    
        char *p; //Pointer of level one
    
        p = &a //assigning the address of variable a
                //to a pointer p of level one
    
        printf("Before incrementing  p: %u",p);
    
    
    
     p++; //incrementing the pointer
    
     printf("\nAfter Incrementing  p:  %u",p);
    }
    

    Output
    Before incrementing p: 6356747

    After Incrementing p: 6356748

    This is what happen when we incremented the char Pointer p:
    p = 6356747 + sizeof(char)
    
    ptr = 6356747 + 1
    
    ptr = 6356748 
    







    Decrement(--)

    • Its Concept is same as increment(++) just inspite of adding 1 it subtracts one from the pointer and gives the address of previous block of same type.

    Decrementing an pointer:
    #include<stdio.h>
    
    void main()
    {
        int a = 10; //integer variable
    
        int *p; //Pointer of level one
    
        p = &a; //assigning the address of variable a
                //to a pointer p of level one
    
        printf("Before Decrementing  p: %u",p);
    
    
     p--; //Decrementing the pointer
    
     printf("\nAfter Decrementing  p:  %u",p);
    }
    

    Output
    Before Decrementing p: 6356744

    After Decrementing p: 6356740

    Formula:
    ptr = ptr - sizeof(datatype_pointer)
    

    This is what happen when we Decremented the Pointer ptr:
    ptr = 6356744 + sizeof(int)
    
    ptr = 6356744 - 4
    
    ptr = 6356740 
    





    Adding and Subtracting a number from the Pointer.

    • As you know that how pointer get add and subtract.
    • Now we will add or subtract number from a Pointer.

    Adding a number from a pointer:
    #include<stdio.h>
    void main()
    {
        int a = 5; //int variable
    
        int *p; //Pointer of level one
    
        p = &a; //assigning the address of variable a
                //to a pointer p of level one
    
        printf("Before incrementing  p: %u",p);
    
    
    
     p = p+2; //Adding 2 to pointer p
    
     printf("\nAfter Adding   p + 2 =  %u",p);
    }
    

    Output
    Before incrementing p: 6356744

    After Adding p + 2 = 6356752

    Formula:
    ptr = ptr + number*sizeof(datatype_pointer)
    

    This is what happen when we added 2 to the Pointer p:
    p = 6356744 + 2*sizeof(int)
    
    p = 6356744 + 2*4
    
    p = 6356744 + 8
    
    p = 6356752 
    

    • Similarly ,we can Subtract a number from a pointer.
    • Whole concept will be same only you have to just subtract a number instead of adding a number






    Further Concept:




    Further Topics:



    People Also Searched: