0

when i subtarct adresses of int type array in two different ways it give me different answers look the code below

#include<stdio.h>
main()
{
int b,c,d,e;
int a[]={1,2,3,4,5,6};
b=&a[0];//b=6422184
c=&a[3];//c=6422196
d=&a[3]-&a[0];//**d=3 in spite of 12, is this an exception or how this statement execute**
e=c-b;//**c=12 when i assign in into int variable and then subtract
printf("value of b=%d\n",b);
printf("value of c=%d\n",c);
printf("value of d=%d\n",d);
printf("value of e=%d\n",e);
//printf("value of f=%d\n",f); 
}

so what is the difference between this two statements
1.d=&a[3]-&a[0];
d=3 and if i store their adrresses in varible then result is changed
2. b=&a[0];
c=&a[3];
e=c-b; e=12 why

how this statement executes or compile i don't understand sorry if i do some silly mistake on my code

  • 4
    Google "pointer arithmetic". `&a[3]-&a[0]` is pointer arithmetic. `c-b` is integer arithmetic. – kaylum Jan 20 '22 at 11:28

0 Answers0