C ++ Pointer Assignment

Why is 90 output value of y and q ? I just do p=q . How has the q value 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; } 

Output:

 35 90 90 90 Address of p = 0xbffa83c0 Address of q = 0xbffa83c0 
+24
c ++ pointers
Aug 15 '11 at 8:15
source share
9 answers

I would like to share a general technique that I used to find out how pointers work when I start. If you apply it to your problem, you will see the answer as simple as a day.

Get a large sheet of graphic paper and lay it along the table in front of you. This is your computer memory. Each box is one byte. Select the line and place the number "100" under the box on the left. This is the "lowest address" memory. (I chose 100 as an arbitrary number that is not 0, you can choose another.) Place the fields in ascending order from left to right.

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

Now, for now, pretend that int is one byte. You are an eight-bit computer. Write int a in one of the fields. The number under the field is its address. Now select another block containing int *b = &a . int *b also a variable stored somewhere in memory, and it is a pointer containing &a , which is pronounced "address".

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

Now you can use this model to visually work with any other combinations of values ​​and pointers that you see. This is a simplification (because, as the words of the pedants say, the pointer is not necessarily an address, and the memory is not necessarily sequential, and there is a stack, heap and registers, etc.), but this is a pretty good analogy for 99% of computers and microcontrollers.

So in your case

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

Now what is *p ? What is *q ?

+76
Aug 15 '11 at 8:20
source share

Because q is the address of y . And after p=q , p also becomes the address of y . This is why p and q print the same address when printing with cout .

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

+7
Aug 15 '11 at 8:18
source share

The q value has not changed; q still points to y . However, p points to y also after p = q , so *p is essentially y , and *p = 90 assigned to y .

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

So your code works as follows:

 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, ie, 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; } 
+4
Aug 15 '11 at 8:19
source share

Take a good look at this after each step:

 int x; int y; 

Now we have two variables x and y :

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

Two more variables are declared, the pointer p , which points to the variable x and contains its address, and the pointer q , which points to the variable y and contains its address:

 x = 35; y = 46; 

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

 p = q; 

Now you assign the address stored in q to the variable p , so both variables point to the address in q , which is the address y :

 *p = 90; 

Here you are looking for p , which is a variable at an address in p , and that is y , and you assign the value 90 variable y .

+4
Aug 15 '11 at 8:24
source share

after executing 'p = q;' operator, two pointers point to the same variant of 'y'. Therefore, when executing '* p = 90;' the value of option 'y' changes.

+2
Aug 15 '11 at 8:21
source share
 int x;int y;int *p = &x;int *q = &y;x = 35;y = 46; 

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

Print x, y, i.e. 35 and 90

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

p and q indicate the same thing: y is the value 90, therefore, 90 and 90 are output

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

Since p and q are the same address, the same value is returned.

+2
Aug 15 '11 at 8:23
source share

See annotations:

 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; } 
+2
Aug 15 '11 at 8:30
source share

When you set p=q , they both refer to 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 y , *p and *q same.

+1
Aug 15 '11 at 8:21
source share

First you define p as a pointer that points to x. And then define q as a pointer indicating y. Then you wrote p = q, so now p and q will both point to y.

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

Now you have the following:

  • y: 90
  • p points to y
  • q points to y
  • * p: 90
  • * q: 90
+1
Aug 15 '11 at 8:23
source share



All Articles