开发者

C++ pointer assignment

开发者 https://www.devze.com 2023-03-27 22:36 出处:网络
Why is 90 开发者_如何学Cthe output value of y and q? I just do p=q. How come the value of q is changed?

Why is 90 开发者_如何学Cthe 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;
}

The output is:

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.

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

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

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.

So in your case,

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

Now what is *p? What is *q?


Because q is the address of y. And after p=q, p also becomes the address of y. That is why p and q print the same address when you print them using cout.

In other words, both p and q point to the same variable y. So if you change the value of any of y, *p or *q, then the change will occur in all, because all of them are same!


Well let's look at it after each step:

int x;
int y;

Now we have two variables x and y:

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

There are declared another two variables, pointer p which points to variable x and contains its address and pointer q which points to variable y and contains its address:

x = 35;
y = 46;

Here you assign values to the variables, this is clear:

p = q;

Now you assign address stored in q to variable p so both variables points to address in q what is address of y:

*p = 90;

Here you dereference p, that is variable on address in p and it is y and you assign value 90 to variable y.


The value of q didn't change, q still points to y. However, p points to y too after p = q, so *p is essentially y, and *p = 90 assigns to y.

Note that cout << "Address of p = " << p << endl; is misleading: p and address of p are two different beasts.

So your code runs like this:

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


See anotations:

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


after executing 'p = q;' statement, the two pointer point to the same variant 'y'. So when executing '*p = 90;', value of the variant 'y' is changed.


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

I.e p points to x (35) and q points to y (46)

p = q;

Now p points to y (46)

*p = 90;

Now the contents of p (aka y) = 90

Now x = 35, y = 90, p and q point to y

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

Prints x, y i.e. 35 and 90

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

p and q point to the same thing - y - whose value is 90 hence 90 and 90 is output

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

As p and q are the same address will output the same value.


When you set p=q, they both reference the same memory location. Therefore, if you change the value pointed to by p, it will also change the value pointed to by q, which is the address of y. Therefore, the output of y, *p and *q are same.


You first define p as a pointer which points to x. And then define q as a pointer pointing y. Then, you wrote p=q, so now, p and q both will point to y.

OK, changing *p, means changing y. then you assign 90 to y by the line *p=90;

Now, you have this:

  • y : 90
  • p points to y
  • q points to y
  • *p : 90
  • *q : 90


First of all let me explain your flow. You put reference of x in *p and reference of y in *q after that you assign values to both x and y. Now your flaw starts here lets say address of x is abc and y is xyz, in your example context you assign q to p means value of q is xyz now p and q both pointing xyz as soon as you put value 90 on address xyz so it prints 90 on both q and p


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

精彩评论

暂无评论...
验证码 换一张
取 消