Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A newbie's question about pointer

Status
Not open for further replies.

Wuu

Technical User
Nov 18, 2007
33
US
Hi,

I am learning pointer right now. I came across a piece of sample code from the following website


I wil paste the code here.

Source:
--------------------------------------------------------
// 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;
}
-----------------------------------------------------------
Results:
firstvalue is 10
secondvalue is 20
-----------------------------------------------------------
Now, I understand the first two statements where the addresses of variables firstvalue and secondvalue are assigned to pointers p1 and p2, respectively.

In the third statement, I think what happens is the value of 10 is assigned to the address or memory location of firstvalue, which therefore should store the value of 10, even though it is initialized with the value of 5.

In the fourth statement, the memory location of the variable firstvalue, which is 10 right now, is assigned to the memory location of the variable secondvalue, which , I believe should hold the value of 10, also, even though it was initially 15.

Based on my understanding, both firstvalue and secondvalue store the value of 10.

In the statment, the memory location of second value is assigned to the pointer p1. So the pointer p1 at this moment should contains the reference to variable secondvalue.

The last statement assigns the value of 20 to the memory location of secondvalue, which therefore obtains the value of 20.

Even though I did come up with the correct results, I am still not sure if my analysis was correct. Pointer has been a way too asbtract concept for me.

I sincerely look forward to any help and comsents.


thanks
 
I think you understand exactly what is going on. At first I thought your terminology was a little confusing, but re-reading it I think you're fine.

Sometimes it helps to draw pictures to make the abstract concept more concrete. Draw the firstvalue, secondvalue, p1 and p2 variables in memory and the values assigned to those variables. Then change the picture as appropriate each step along the way.
 
Thanks, uolj.

I haven't quite aquired the way an expert or professional programmer would use when discussing a piece of code. I wonder if I would ever talk like a pro. I usually need to spend some time thinking about term's definition when I come across one.

Yeah, I did realize drawing help a lot. Thanks.

 
Hi, uolj,

I would appreciate it very much if you could help identify the places where I have used a confusing terminology.


Thanks
 
I said I was confused at first, but after re-reading it you did fine.

You use the term memory location, and I often think of what you're referring to as the value or data of the memory location, so I at first thought you meant the address rather than the data. But after re-reading it I see that you meant the data at the memory location.

You don't necessarily need to change your terminology. Mine might be more confusing than yours.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top