0

Consider the code:

int arr[20]{};
int * ptr1=arr, * ptr2=&arr[1];
std::cout<<ptr1<<std::endl<<ptr2<<std::endl<<ptr2-ptr1;

Output:

0x7fff4003e0d0
0x7fff4003e0d4
1

Why it isn't 4 instead?

dlpsankhla
  • 132
  • 1
  • 2
  • 9

1 Answers1

0

If it didn't to that you'd just end up dividing the difference by sizeof(whatever) all the time. The number of elements is far more useful than the raw difference. When you need the latter, cast the two pointers to char* for the subtraction.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165