我很困惑。 secondvalue如何= 20?在评论中是我认为正在发生的事情,我在某处丢失了什么?指针帮助C++
int firstvalue = 5, secondvalue = 15;
int *p1, *p2;
p1 = &firstvalue; //assign the address of firstvalue to p1
p2 = &secondvalue; //assign... secondvalue to p2
*p1 = 10; //assign 10 to the value pointed by p1 (firstvalue now = 10)
*p2 = *p1; //assign the value pointed by p1 to the value pointed by p2
//secondvalue = 10, firstvalue = 10
p1 = p2; //assign the address of secondvalue to the address of firstvalue
//address of firstvalue = address of secondvalue
*p1 = 20; //assign 20 to the value pointed by p1 (firstvalue = 20)
输出应该 firstvalue = 10和secondvalue = 20,但是从我的理解firstvalue = 20,secondvalue = 10.在哪里我会错呢?
你是否运行过代码?结果是什么? – Igor
您在p1 = p2的意见不正确。此声明将secondValue的地址分配给p1,而不是第一个值的地址 –