理解指针教程的问题

时间:2015-07-19 15:03:23

标签: c++ pointers

此代码取自cplusplus.com。它在教程中用于解释指针,我很难理解。

当他们写p1 = 20时;在代码的末尾,不会改变第一个值的内存地址以及第二个值吗?因为他们之前已经在代码中创建了p1 = p2。

如果有人采用不同的方式解释它,不同于他们对网站或其他教程的评论吗?

谢谢!

#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 to by p1 = 10
  *p2 = *p1;         // value pointed to by p2 = value pointed to by p1
  p1 = p2;           // p1 = p2 (value of pointer is copied)
  *p1 = 20;          // value pointed to by p1 = 20

  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
}

取自&#34; http://www.cplusplus.com/doc/tutorial/pointers/&#34;

3 个答案:

答案 0 :(得分:5)

图表可能更容易解释这一点。

  int firstvalue = 5, secondvalue = 15;

这意味着在内存中的某个位置创建了两个int个对象:

+------------+    +-------------+
|     5      |    |      15     | 
+------------+    +-------------+
  int * p1, * p2;

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

这意味着创建了两个指针对象。它们的值是这样的,它们指向两个int对象:

+------------+    +-------------+
|     5      |    |      15     | 
+------------+    +-------------+
       ^                 ^
       |                 |
    +----+            +----+
    | p1 |            | p2 |
    +----+            +----+
*p1 = 10;          // value pointed to by p1 = 10

这意味着修改了p1指向的对象,即第一个int对象:

+------------+    +-------------+
|     10     |    |      15     | 
+------------+    +-------------+
       ^                 ^
       |                 |
    +----+            +----+
    | p1 |            | p2 |
    +----+            +----+
*p2 = *p1;         // value pointed to by p2 = value pointed to by p1

int指向的p2对象应设置为与p1指向的对象相同的值:

+------------+    +-------------+
|     10     |    |      10     | 
+------------+    +-------------+
       ^                 ^
       |                 |
    +----+            +----+
    | p1 |            | p2 |
    +----+            +----+
p1 = p2;           // p1 = p2 (value of pointer is copied)

在此行之后,没有任何内容指向第一个int对象了。 p1指向与p2相同的对象:

+------------+      +-------------+
|     10     |  +-> |      10     | 
+------------+  |   +-------------+
                |          ^
                |          |
    +----+      |       +----+
    | p1 |------+       | p2 |
    +----+              +----+
*p1 = 20;          // value pointed to by p1 = 20

p1指向的对象(恰好与p2指向的对象)设置为20:

+------------+      +-------------+
|     10     |  +-> |      20     | 
+------------+  |   +-------------+
                |          ^
                |          |
    +----+      |       +----+
    | p1 |------+       | p2 |
    +----+              +----+

我们走了。第一个int是10,第二个是20。

答案 1 :(得分:1)

  

当他们写p1 = 20时;在代码的末尾,是否会改变第一个值的内存地址以及第二个值?因为他们之前已经在代码中创建了p1 = p2。

他们实际上写了*p1 = 20,但这就是为什么你是对的,是的。

如果您感到困惑,因为该程序仍然会发出两个不同的值,那是因为您正在打印firstvaluesecondvalue(仍然不同),而不是*p1和{ {1}}(到那时为*p2)。

你没有解释为什么这会让你感到困惑或者你的问题是什么,或者这与secondvalue s有什么关系,所以这就是我可以告诉你的。

答案 2 :(得分:0)

我已就您的理解发表评论

#include <iostream>
using namespace std;

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

  p1 = &firstvalue;  // *p1 = 5, since p1 is pointing to firstvalue
  p2 = &secondvalue; // *p2 = 15, since p2 is pointing to secondvalue
  *p1 = 10;          // *p1=10, changing firstvalue to 10
  *p2 = *p1;         // now *p2=10;
  p1 = p2;           // p1 is now pointing to secondvalue whose value is already changed into 10 by *p2=*p1; statement.
  *p1 = 20;          // now secondvalue=20 since p1 is pointing to second value

  cout << "firstvalue is " << firstvalue << '\n'; //prints 10
  cout << "secondvalue is " << secondvalue << '\n'; //prints 20
}