+ 1
C++ Pointers
is there a difference between *p1=*p2 and p1=p2? example in the following code: #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 p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed to by p1 = 20 cout << "firstvalue is " << firstvalue << '\n'; // Outputs 10 cout << "secondvalue is " << secondvalue << '\n'; // Outputs 20 return 0; }
2 Respuestas
0
Yes.
p1=p2 copies the adress that is stored in p2 to p1.
*p1 gives you the VALUE of the variable wich is stored in p1.
Example:
Int v1=1;
Int *p1 =&v1;
cout << *p1; gives you "1"
cout << p1; gives you the adress of v1. (Smth like "0x2517af")
0
but the result will be the same. right ?
the action is different but it causes the same result.
if you do
p1=p2
or
*p1 =*p2.
in both cases after - cout- value is copied.
yes ?