When subtracting two pointers in C

后端 未结 5 1988
星月不相逢
星月不相逢 2021-01-07 12:18

I was playing with pointers in order to fully get the concept and then wanted to subtract two pointers expecting the distance between these two addresses or something, but a

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 12:50

    The subtraction of 2 pointers give the distance in between the 2 variables. For eg.

    //let the address of a is 1000 then of a+1 will be 1004

    int a[]={1,2,3};
    int *p1=a;
    int *p2=a+1;
    printf("%u",p2-p1);
    

    The result of this will be 1 not 4. Same here in your case the the location of x and y are consecutive that is why the ans. is 1.

提交回复
热议问题