#include <stdio.h>
int main() {
int c[6] = { 10,20,30,40,50,60 }, * p, * s;
p = c; s = &c[5];
printf("%d\n", s - p);
}
地址相减最后的结果为什么不是20
结果不可能是20的,结果应该是5,因为s和p都是指针,print打印的为指针之间的差值,几位移量5-0等于5!望采纳!
p相当于c[0]的地址,s是c[5]的地址,两者差的意思是几个整型
你要改成printf("%d\n",(char*)s - (char*)p);那么结果就是20了
因为编译器会根据你的指针类型按照类型的字节数做差,你想真实地址的差,要用void指针
#include <stdio.h>
int main() {
int c[6] = { 10,20,30,40,50,60 };
void * p = c;
void * s = &c[5];
printf("%d\n",s - p);
}