我大致了解了指针的逻辑,但对我来说有一个不清楚的点与底层代码有关。
#include <iostream>
using namespace std;
int main ()
{
int first = 50,
second = 150;
int * p1, * p2;
p1 = &first; //p1 is assigned to the address of first
p2 = &second; //p2 is assigned to the address of second
*p1 = 100; //first's value is changed as 100 by assigning p1's value to 100
*p2 = *p1; //now p2's value should be 100
p1 = p2; //I think we want to change p1's adress as p2
*p1 = 200; //I expect that the new value of p1 should be 200
cout << first << second;
return 0;
}
程序打印 first=100 和 second=200,但正如我上面评论的,我希望 p1 的值更改为 200。但它仍然保持为 100。那有什么意义?