Don't understand this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// more pointers
#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 by p1 = 10
  *p2 = *p1;         // value pointed by p2 = value pointed by p1
  p1 = p2;           // p1 = p2 (value of pointer is copied)
  *p1 = 20;          // value pointed by p1 = 20
  
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}


Could someone explain this code in detail please? I felt that the tutorial briefly touched this subject. Anyways, I understand this much. P1 and P2 are equal to the address of the first and second values. The value pointed to be P1 is 10, so in turn the first value must equal 10. Then the value pointed to by P2 is equal to the value pointed by P1, I think. So this would make the second value 10 too, right? Then the tut says that by "p1 = p2", the value is copied, but isn't the address copied? How in the world does p1 end up pointing to the value of 20?
P1 and P2 are equal to the address of the first and second values.

they are not equal to the value, but they point to the memory space that holds the values of firstvalue and secondvalue.

The value pointed to be P1 is 10, so in turn the first value must equal 10.

correct, because p1 points to firstvalue memory space, it changes the value of firstvalue to 10

Then the value pointed to by P2 is equal to the value pointed by P1, I think. So this would make the second value 10 too, right?

correct, it basically says copy what is in the memory space p1 points to into the memory space p2 points to. because the value p1 points to is 10, now the value p2 points to is 10.

Then the tut says that by "p1 = p2", the value is copied, but isn't the address copied?

your are right, the address is copied not the value. So now p1 points to the memory space that p2 does(and the value of that space)

How in the world does p1 end up pointing to the value of 20?

*p1 = 20; is setting the value of the memory space that p1 points to, to the value of 20. Because p1 now points to p2's memory space, this in turn changes the value that p2 points to as well.

Remember that a pointer does not hold any values. It just points to a memory space of the same data type.
Last edited on
If P1 now points to the address AND value of P2, then it would remain 10 wouldn't it? *p2 and *p1 = 10

If it would, I still don't see where 20 comes from. When did we ever add?
It's already answered for you...
1
2
    p1 = p2;    //let p1 have the address that p2 is pointing to. This does not make p1 point to the value of p2. It just make p1 point to where p2 is pointing at.
    *p1 = 20;   //Dereference p1 and assign a new value to it. This changes what p1 is pointing to. However, at this line, p1 is pointing to the data pointed by p2, so if you dereference p2, you'll see the value change. 
Oh! So there is no addition or anything, we just changed the value of what P1 is pointing to, which in turn changes the value of P2 because of P1 = P2.

If this is correct, then I was making it was more difficult than it really was.
Topic archived. No new replies allowed.