Question about Pointers

Quick question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #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 to by p1 = 10
  *p2 = *p1;         // value pointed to by p2 = value pointed to by p1
  p1 = p2;           // p1 = p2 (value of pointer is copied)
  *p1 = 20;          // value pointed to by p1 = 20

  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  return 0;
}


Output:
firstvalue is 10
secondvalue is 20


I don't understand how firstvalue prints 10 and secondvalue prints 20.
Shouldn't firstvalue be 20 because in the end *p1 = 20 and secondvalue be 10 because *p2 = *p1 while *p1 = 10?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

int main ()
{
    int firstvalue = 25 ;
    int secondvalue = 15;
    int* p1 = std::addressof(firstvalue) ;
    int* p2 = std::addressof(secondvalue) ;

    const auto points_to = [&] ( const int* pointer )
    {
        if( pointer == std::addressof(firstvalue) ) return " (points to firstvalue)" ;
        else if( pointer == std::addressof(secondvalue) ) return " (points to secondvalue)" ;
        else return "" ;
    };

    const auto dump = [&] ( const char* desc )
    {
      std::cout << desc << "\n------------------\n"
                << " firstvalue: has address " << std::addressof(firstvalue) << " value " << firstvalue << '\n'
                << "secondvalue: has address " << std::addressof(secondvalue) << " value " << secondvalue << '\n'
                << "         p1:   points to " << p1 << " value " << *p1 << points_to(p1) << '\n'
                << "         p2:   points to " << p2 << " value " << *p2 << points_to(p2) << "\n\n" ;
    };

    dump( "after initialisation") ;
    *p1 = 10; dump( "after *p1 = 10" ) ;
    *p2 = *p1;  dump( "after *p2 = *p1" ) ;
    p1 = p2;  dump( "after p1 = p2" ) ;
    *p1 = 20; dump( "after *p1 = 20" ) ;
}

after initialisation
------------------
 firstvalue: has address 0x7fff27c1abbc value 25
secondvalue: has address 0x7fff27c1abb8 value 15
         p1:   points to 0x7fff27c1abbc value 25 (points to firstvalue)
         p2:   points to 0x7fff27c1abb8 value 15 (points to secondvalue)

after *p1 = 10
------------------
 firstvalue: has address 0x7fff27c1abbc value 10
secondvalue: has address 0x7fff27c1abb8 value 15
         p1:   points to 0x7fff27c1abbc value 10 (points to firstvalue)
         p2:   points to 0x7fff27c1abb8 value 15 (points to secondvalue)

after *p2 = *p1
------------------
 firstvalue: has address 0x7fff27c1abbc value 10
secondvalue: has address 0x7fff27c1abb8 value 10
         p1:   points to 0x7fff27c1abbc value 10 (points to firstvalue)
         p2:   points to 0x7fff27c1abb8 value 10 (points to secondvalue)

after p1 = p2
------------------
 firstvalue: has address 0x7fff27c1abbc value 10
secondvalue: has address 0x7fff27c1abb8 value 10
         p1:   points to 0x7fff27c1abb8 value 10 (points to secondvalue)
         p2:   points to 0x7fff27c1abb8 value 10 (points to secondvalue)

after *p1 = 20
------------------
 firstvalue: has address 0x7fff27c1abbc value 10
secondvalue: has address 0x7fff27c1abb8 value 20
         p1:   points to 0x7fff27c1abb8 value 20 (points to secondvalue)
         p2:   points to 0x7fff27c1abb8 value 20 (points to secondvalue)

http://coliru.stacked-crooked.com/a/722921ebf305b122
Last edited on
Topic archived. No new replies allowed.