Possible Duplicate:
Pointer Arithmetic In C
Code:
int main()
{
int a[ ] ={0,1,2,3,4};
char c[ ] = {'s','a','n','j','u'};
printf("%d\n",&a[3]-&a[0]);
printf("%d\n",&c[3]-&c[0]);
return 0;
}
Why the output comes 3 3 for both, if we consider the difference in addresses they will be different for both??
In pointer arithmetics, subtraction return the difference not in bytes, but in the pointer’s type between two pointers.
So, since the difference in
ints betweena[3]anda[0]is identical to the difference inchars betweenc[3]andc[0]– you get the same result for both.The arithmetics for pointers subtraction operation is something like: