Tracing this program with pointers?

Apr 16, 2015 at 4:11am
Hi,

Im trying to trace the following program (by hand) given in the tutorials section of this website. However, Im ending up with different answers than the ones given.

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 by p1
  p1 = p2;           // p1 = p2 (value of pointer is copied)
  *p1 = 20;          // value pointed by p1 = 20
  
  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  return 0;
}


I actually get 10 for BOTH firstvalue and secondvalue. Obviously there is something wrong with my logic of my tracing, but I dont know what.

Can someone explain to me step-by-step how the answer 10 and 20 is reached? Keep in mind that I am VERY new to pointers.


Apr 16, 2015 at 4:25am
What do you think line 13 is doing? What about line 14?

It may interest you to know that you can (and probably should) avoid pointers entirely for the most part:
http://www.LB-Stuff.com/pointers
Last edited on Apr 16, 2015 at 4:26am
Apr 16, 2015 at 4:37am
What do you think line 13 is doing? What about line 14?


Thats actually where I think im getting confused.

Here's what I think is happening (and of course, please correct me if im wrong).

Line 13 is an assignment statement that assigns the address of secondvalue (whatever that address is) to the address of firstvalue.

So lets say the memory address of firstvalue is 700 (just made that up, for examples sake), and the address of secondvalue is 800. After line 13, p1 (address of first value) now beoomes 800.

So far we now have firstvalue =10, secondvlaue=10, p1 = 800 and p2=800.

In line 14, the value pointed to by p1 is said to be 20. Here is where Im stuck. How does secondvalue become 20 now?
Apr 16, 2015 at 4:42am
After line 13, p1 and p2 both point to secondvalue. It doesn't matter which of those three you use - assigning 20 will assign 20.
Apr 16, 2015 at 4:49am
Oh, I think I understand now.

So basically, line 13 is saying that p1 is now pointing to whatever p2 is pointing at. p2 is pointing at the memory address of secondvalue.

In line 14, the value pointed to by p1 is 20. Well, p1 is pointing to the same thing as p2, address of secondvalue. And so the value of secondvalue now becomes 20.

Am I correct in my understanding?
Apr 16, 2015 at 4:53am
I think so, you just use odd wording. Instead of saying "the value pointed to by p1 is 20" you should say "20 is assigned to the value pointed to by p1". When you see =, think "value on right is assigned to thing on left".
Last edited on Apr 16, 2015 at 4:58am
Apr 16, 2015 at 4:56am
Gotcha.

Thank you so much.
Apr 16, 2015 at 5:00am
Quick note, "is assigned to" and "is set to" are opposites in terms of the order. x = y assigns y to x but sets x to y. Just in case there was any confusion.
Topic archived. No new replies allowed.