当两个值具有相同的地址时

时间:2013-05-14 06:43:53

标签: c++

#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.

6 个答案:

答案 0 :(得分: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

答案 1 :(得分: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都指向它

答案 2 :(得分:2)

使用操作p1 = p2;,您正在更改指针p1,而不是firstvalue的地址。 p1不再指向firstvalue,而是指向secondvalue

答案 3 :(得分: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 = 20firstvalue = 10, secondvalue = 20

答案 4 :(得分:0)

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

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

cout << "firstvalue is " << *p1 << endl;
cout << "secondvalue is " << *p2 << endl;

答案 5 :(得分: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

正如其他答案中所提到的,你现在有两个指向同一个变量的指针。 first value不会改变*p1 = 20,因为p1指向second value而不是first value