C pointer arithmetic

后端 未结 5 1773
夕颜
夕颜 2020-12-05 11:32

Given this code:

int *p, *q;

p = (int *) 1000;
q = (int *) 2000;

What is q - p and how?

5条回答
  •  忘掉有多难
    2020-12-05 12:22

    q-p supposed to return how many steps with increment you should do to go from p to q. Which is 1000 / sizeof(int) and equals 250. Remember q++ will actually go to the next element of type int, not in the middle of it, so it should add 4 to the actual value of the pointer. Hence the result.

提交回复
热议问题