Pointer Program : Difference between two integer Pointers

January 1, 2025 No Comments » Hits : 180





Program :

#include<stdio.h>
int main(){
int *ptr1=(int *)1000;
int *ptr2;
ptr2 = ptr1 + 2;
printf("\nAddress of Ptr1 = %u",ptr1);
printf("\nAddress of Ptr2 = %u",ptr2);
printf("\nDifference : %d",ptr2-ptr1);
return 0;
}

Output :

Address of Ptr1 = 1000
Address of Ptr2 = 1004
Difference : 2

Explanation :

  • Ptr1 and Ptr2 are two pointers which holds memory address of Integer Variable.
  • Ptr2-Ptr1 will gives us number of Integers that can be stored.
ptr2 - ptr1 = (1004-1000)/sizeof(int)
            = 4 / 2
            = 2

Incoming search terms: