C ++指针分配 [英] C++ pointer assignment

查看:73
本文介绍了C ++指针分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么90yq的输出值?我只是做p=q. q的值如何更改?

Why is 90 the output value of y and q? I just do p=q. How come the value of q is changed?

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;
}

输出为:

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

推荐答案

我想分享一种通用技术,当我刚开始学习该技术时,该技术用于学习指针的工作方式.如果将其应用于您的问题,您将看到每天的答案.

I'd like to share a general technique that I used to learn how pointers work when I was starting out. If you apply it to your problem, you'll see the answer as plain as day.

获取一大张方格纸,然后将其纵向放在您面前的桌子上.这是您计算机的内存.每个方框代表一个字节.选择一行,并将数字"100"放置在最左侧的框下方.这是内存的最低地址". (我选择100作为不为0的任意数字,您可以选择另一个.)以从左到右的升序对框进行编号.

Get a big sheet of graph paper and lay it lengthwise on the table in front of you. This is your computer's memory. Each box represents one byte. Pick a row, and place the number '100' below the box at far left. This is "the lowest address" of memory. (I chose 100 as an arbitrary number that isn't 0, you can choose another.) Number the boxes in ascending order from left to right.


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

现在,就目前而言,假设一个int大小为一个字节.您是八位计算机.将您的int a写到其中一个框中.框下面的数字是它的地址.现在,选择另一个包含int *b = &a的框. int *b也是存储在内存中某个位置的变量,它是一个包含&a的指针,其发音为"a的地址".

Now, just for the moment, pretend an int is one byte in size. You are an eight-bit computer. Write your int a into one of the boxes. The number below the box is its address. Now choose another box to contain int *b = &a. int *b is also a variable stored somewhere in memory, and it is a pointer that contains &a, which is pronounced "a's address".

int  a = 5;
int *b = &a;


  a       b 
+---+---+---+---+---+--
| 5 |   |100|   |   | ...
+---+---+---+---+---+--
 100 101 102 103 104  ...

现在,您可以使用此模型来直观地查看看到的值和指针的任何其他组合.这是一种简化(因为语言专家会说,指针不一定是地址,内存不是顺序的,并且有堆栈,堆和寄存器等等),但是对于99%的计算机和微控制器来说,这是一个很好的类比.

Now you can use this model to visually work through any other combinations of values and pointers that you see. It is a simplification (because as language pedants will say, a pointer isn't necessarily an address, and memory isn't necessarily sequential, and there's stack and heap and registers and so on), but it's a pretty good analogy for 99% of computers and microcontrollers.

所以在您的情况下

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?

Now what is *p? What is *q?

这篇关于C ++指针分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆