繁体   English   中英

C++ 指针赋值

[英]C++ pointer assignment

为什么yq的 output 值是90 我只是做p=q q的值怎么变了?

int main() 
{

    int x;
    int y;
    int *p = &x;
    int *q = &y;

    x = 35;
    y = 46;

    p = q;

    *p = 90;

    cout << x << " " << y << endl;
    cout << *p << " " << *q << endl;
    cout << "Address of p = " << p << endl;
    cout << "Address of q = " << q << endl;

    return 0;
}

output 是:

35 90
90 90
Address of p = 0xbffa83c0
Address of q = 0xbffa83c0

我想分享一个通用技术,当我开始学习指针的工作原理时,我使用该技术。 如果你把它应用到你的问题上,你会看到答案一清二楚。

拿一张大方格纸,将它纵向放在你面前的桌子上。 这是您计算机的 memory。 每个框代表一个字节。 选择一行,然后将数字“100”放在最左边的框下方。 这是 memory 的“最低地址”。 (我选择了 100 作为非 0 的任意数字,您可以选择另一个。)从左到右按升序对框进行编号。

+---+---+---+---+---+--
|   |   |   |   |   | ...
+---+---+---+---+---+--
100  101 102 103 104  ...

现在,暂时假设一个 int 大小为一个字节。 你是一台八位计算机。 将您的int a写入其中一个框中。 方框下方的数字是它的地址。 现在选择另一个框来包含int *b = &a int *b也是存储在 memory 某处的变量,它是一个包含&a的指针,读作“a 的地址”。

int  a = 5;
int *b = &a;
  a       b 
+---+---+---+---+---+--
| 5 |   |100|   |   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...

现在,您可以使用此 model 直观地处理您看到的任何其他值和指针组合。 这是一种简化(因为正如语言学究所说,指针不一定是地址,memory不一定是顺序的,还有堆栈、堆和寄存器等等),但对于 99% 来说,这是一个很好的类比计算机和微控制器。

所以在你的情况下,

int x = 35;
int y = 46;
  x   y 
+---+---+---+---+---+--
| 35| 46|   |   |   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...
int *p = &x;
int *q = &y;
  x   y   p   q
+---+---+---+---+---+--
| 35| 46|100|101|   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...
p = q;
  x   y   p   q
+---+---+---+---+---+--
| 35| 46|101|101|   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...
*p = 90;
  x   y   p   q
+---+---+---+---+---+--
| 35| 90|101|101|   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...

现在*p是什么? 什么是*q

因为qy的地址。 p=q之后, p 也成为y的地址。 这就是为什么pq在使用cout打印它们时会打印相同的地址。

换句话说, pq都指向同一个变量y 因此,如果您更改任何y*p*q的值,那么更改将全部发生,因为它们都是相同的!

好吧,让我们在每一步之后看看它:

int x;
int y;

现在我们有两个变量xy

int *p = &x;
int *q = &y;

声明了另外两个变量,指向变量x并包含其地址的指针p和指向变量y并包含其地址的指针q

x = 35;
y = 46;

在这里你为变量赋值,这很清楚:

p = q;

现在您将存储在q中的地址分配给变量p ,因此两个变量都指向q中的地址y的地址是什么:

*p = 90;

在这里,您取消引用p ,即p中地址上的变量,它是y ,您将值90分配给变量y

q没有改变, q仍然指向y 但是, pp = q之后也指向y ,因此*p本质上是y ,并且*p = 90分配给y

注意cout << "Address of p = " << p << endl; 具有误导性: pp的地址是两种不同的野兽。

所以你的代码像这样运行:

int main() {

    int x;
    int y;
    int *p = &x; // now p points to x
    int *q = &y; // now q points to y

    x = 35;
    y = 46;

    p = q;       // now p is the same as q, i.e., points to y

    *p = 90;     // p points to y, so *p is y.
                 // so we assign 90 to y

    cout << x << " " << y << endl;
    cout << *p << " " << *q << endl; // both *p and *q are y
    cout << "Address of p = " << p << endl;
    cout << "Address of q = " << q << endl;

    return 0;
}

见注释:

int main()
{
int x;
int y;
int *p = &x;
int *q = &y;

x = 35;
y = 46;

p = q;      // at this point p is now pointing to the same memory address 
            // as q, both of them are pointing to the memory allocated to y

*p = 90;    // this will also change the values in *q and y 

cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
cout << "Address of p = " << p << endl;
cout << "Address of q = " << q << endl;

return 0;
}

执行 'p = q;' 后语句,这两个指针指向同一个变体'y'。 所以当执行'*p = 90;'时,变量'y'的值会改变。

int x;int y;int *p = &x;int *q = &y;x = 35;y = 46;

即 p 指向 x (35) 和 q 指向 y (46)

p = q;

现在 p 指向 y (46)

*p = 90;

现在 p (aka y) = 90

现在 x = 35, y = 90, p 和 q 指向 y

cout << x << " " << y << endl;

打印 x、y 即 35 和 90

cout << *p << " " << *q << endl;

p 和 q 指向同一事物 - y - 其值为 90,因此 90 和 90 为 output

cout << "Address of p = " << p << endl;cout << "Address of q = " << q << endl;

由于 p 和 q 是相同的地址,所以 output 的值相同。

当您设置p=q时,它们都引用相同的 memory 位置。 因此,如果你改变p指向的值,它也会改变q指向的值,也就是y的地址。 因此, y*p*q的 output 是相同的。

您首先将 p 定义为指向 x 的指针。 然后将 q 定义为指向 y 的指针。 然后,你写了 p=q,所以现在 p 和 q 都指向 y。

OK,改变*p,意味着改变y。 然后通过 *p=90; 行将 90 分配给 y;

现在,你有这个:

  • 是:90
  • p 指向 y
  • q 指向 y
  • *p:90
  • *q: 90

首先让我解释一下你的流程。 将 x 的引用放在 *p 中,将 y 的引用放在 *q 中,然后将值分配给 x 和 y。 现在你的缺陷从这里开始,假设 x 的地址是 abc,y 是 xyz,在你的示例上下文中,你将 q 分配给 p 表示 q 的值现在是 xyz,p 和 q 都指向 xyz,只要你将值 90 放在地址 xyz 上,所以它在 q 和 p 上都打印 90

int main() {

int x;
int y;
int *p = &x;//this p pointer stores address of variable x
int *q = &y;//this q pointer stores address of variable y

x = 35;//assigned value 35 to x
y = 46;//assigned value 46 to y

p = q;//this line make pointer p to store address which is their in q that is address of y

*p = 90;//this line will change the value of y because by previous we change the pointer p address before line p=q p was pointing to x but now it is pointing to y

cout << x << " " << y << endl;
cout << *p << " " << *q << endl;as we make pointers p and q pointing to same variable that is y and change the value of y to 90 by doing*p=90 output for both *p & *q is 90
cout << "Address of p = " << p << endl;
cout << "Address of q = " << q << endl;

return 0;

希望我能够回答这个问题,如果喜欢然后请投票,以便更多人可以看到这个答案谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM