int y=1;
int k=2;
int *p1;
int *p2;
p1=&y;
p2=&k;
p1=p2;
*p1=3;
*p2=4;
printf("%d",y);
我得到的输出为 1,有人能解释一下为什么吗!!我原以为是 4。
最佳答案
下面的评论解释了这是如何工作的:
int y=1;
int k=2;
int *p1;
int *p2;
p1=&y; //pointer p1 holds the address of y
p2=&k; //pointer p2 holds the address of k
p1=p2; //pointer p1 now holds the address which p2 holds, which is the address of k
*p1=3; //the value which p1 points to is now 3 (so k equals 3 as well)
*p2=4; //the value which p2 points to is now 4 (so k equals 4 as well)
printf("%d",y); //y is still 1
但是,如果您执行了 printf("%d",k);
,则会打印值 4
关于c - C中的指针赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496565/