C Subtracting Pointers: Differencing Pointer in C Programming Language
C Subtracting Pointers: Differencing Pointer in C Programming Language
In the previous chapter we have learnt about subtracting the integer value from the pointer
variable. In this chapter we will be computing the difference between two pointer variables.
#include<stdio.h>
int main()
{
int num , *ptr1 ,*ptr2 ;
ptr1 = &num ;
ptr2 = ptr1 + 2 ;
printf("%d",ptr2 - ptr1);
return(0);
}
Output :
Important Observations:
ptr1 = &num ;
1000 Garbage
ptr2 = ptr1 + 2 ;
1000 1004
ptr2 - ptr1
1000 1004
Remember the following formula while computing the difference between two pointers –
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
and
If Two Pointers are of Following Numerical Technical
Data Type Difference Difference
Integer 2 1
Float 4 1
Character 1 1