2
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue = 5, secondvalue = 15;
  int * p1, * p2;

  p1 = &firstvalue;  // p1 = address of firstvalue
  p2 = &secondvalue; // p2 = address of secondvalue
  *p1 = 10;          // value pointed by p1 = 10
  *p2 = *p1;         // value pointed by p2 = value pointed by p1
  p1 = p2;           // p1 = p2 (value of pointer is copied)
  *p1 = 20;          // value pointed by p1 = 20

  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}

输出是 10 20。输出不应该是 20 20 吗?我在想的是因为它确实 p1 = p2 因此 p1 和 p2 具有相同的地址,然后它设置 p1 = 20 指向的值。所以它们的值假设更改为 20。

4

6 回答 6

2

通过该操作p1 = p2;,您正在更改指针p1 而不是firstvalue. p1不再指向firstvalue而是指向secondvalue

于 2013-05-14T06:58:49.463 回答
2
  p1 = &firstvalue;
  p2 = &secondvalue;

  // here, p1 points to first, p2 points to second, first is 5, second is 15

  *p1 = 10;          

  // here, p1 points to first, p2 points to second, first is 10, second is 15

  *p2 = *p1; 

  // here, p1 points to first, p2 points to second, first is 10, second is 10

  p1 = p2;   

  // here, p1 points to second, p2 points to second, first is 10, second is 10

  *p1 = 20;  

  // here, p1 points to second, p2 points to second, first is 10, second is 20
于 2013-05-14T06:48:05.987 回答
2

让我重新表述你的评论:

  p1 = &firstvalue;  // p1 points to firstvalue
  p2 = &secondvalue; // p2 points to secondvalue
  *p1 = 10;          // means firstvalue = 10
  *p2 = *p1;         // means secondvalue = firstvalue, which is 10
  p1 = p2;           // p1 now points to secondvalue and not to firstvalue any more
  *p1 = 20;          // means secondvalue = 20

净效果:firstvalue 是 10,没有指针指向它。secondvalue 是 20,p1 和 p2 都指向它

于 2013-05-14T06:48:56.713 回答
1

让我们逐行执行此操作:

int firstvalue = 5, secondvalue = 15;
int * p1, * p2;

p1 = &firstvalue;  // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue

直到这里,firstvalue = 5, secondvalue = 15.

*p1 = 10;          // value pointed by p1 = 10

现在*p1 = 10,就这样firstvalue = 10, secondvalue = 15

*p2 = *p1;         // value pointed by p2 = value pointed by p1

现在*p2 = secondvalue = *p1 = firstvalue = 10,就这样firstvalue = 10, secondvalue = 10

p1 = p2;           // p1 = p2 (value of pointer is copied)
*p1 = 20;          // value pointed by p1 = 20

而现在*p1 = secondvalue = 20,就这样firstvalue = 10, secondvalue = 20

于 2013-05-14T06:50:00.247 回答
0
 first  second   first  second   first  second   first  second     first  second
 value  value    value  value    value  value    value  value      value  value
 +---+  +---+    +---+  +---+    +---+  +---+    +---+  +---+      +---+  +---+
 |   |  |   |    |   |  |   |    |   |  |   |    |   |  |   |      |   |  |   |
 | 5 |  | 15|    | 10|  | 15|    | 10|  | 10|    | 10|  | 15|      | 10|  | 20|
 +---+  +---+    +---+  +---+    +---+  +---+    +---+  +-^-+      +---+  +-^-+
   ^      ^        ^      ^        ^      ^        +------|          +------|
 +-|-+  +-|-+    +-|-+  +-|-+    +-|-+  +-|-+    +-|-+  +-+-+      +-|-+  +-+-+
 | | |  | | |    | | |  | | |    | | |  | | |    | + |  | | |      | + |  | | |
 | + |  | + |    | + |  | + |    | + |  | + |    |   |  | + |      |   |  | + |
 +---+  +---+    +---+  +---+    +---+  +---+    +---+  +---+      +---+  +---+
  p1     p2       p1     p2       p1     p2       p1     p2         p1     p2

正如其他答案中提到的,您现在有两个指向同一个变量的指针。Your first valueis not changed by*p1 = 20因为p1指向second valuenotfirst value

于 2013-05-14T07:35:36.417 回答
0

Firstvalue 和 secondvalue 不是指针,它们是你的变量,小心!

指针是 p1 和 p2,因此您期望的输出是:

cout << "firstvalue is " << *p1 << endl;
cout << "secondvalue is " << *p2 << endl;
于 2013-05-14T06:46:37.990 回答