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