Differencing or Subtracting two Pointer in C Programming Language

January 8, 2012 No Comments » Hits : 217





Differencing Pointer in C Programming Language :

  1. Differencing Means Subtracting two Pointers.
  2. Subtraction gives the Total number of objects between them .
  3. Subtraction indicates “How apart the two Pointers are “

Example :

#include<stdio.h>

int main()
{
int num , *ptr1 ,*ptr2 ;

ptr1 = &num ;
ptr2 = ptr1 + 2 ;

printf("%d",ptr2 - ptr1);

return(0);
}

Output :

 2

  • ptr1 stores the address of Variable num
  • Value of ptr2 is incremented by 4 bytes
  • Differencing two Pointers

Observations & Explanation :

  1. Numerically Subtraction ( ptr2-ptr1 ) differs by 4
  2. As both are Integers they are numerically Differed by 4 and Technically by 2 objects
  3. Suppose Both pointers of float the they will be differed numerically by 8 and Technically by 2 objects

Incoming search terms: