Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualYoubar

Posted 04 December 2012 - 07:12 PM

// 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;
}

I understand all but this line of the script, which I don't see any reason for:
p1 = p2;		   // p1 = p2 (value of pointer is copied)

I can see that it says it was copied, but I thought that already happened in the line above that?
*p2 = *p1;		 // value pointed by p2 = value pointed by p1

#2Youbar

Posted 04 December 2012 - 07:07 PM

// 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;
}

I understand all but this line of the script, which I don't see any reason for:
p1 = p2;		   // p1 = p2 (value of pointer is copied)

I can see that it says it was copied, but I thought that already happened in the line above that?
*p2 = *p1;		 // value pointed by p2 = value pointed by p1

#1Youbar

Posted 04 December 2012 - 07:06 PM

// 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;
}

I understand all of the script, but I don't see any reason for:
p1 = p2;		   // p1 = p2 (value of pointer is copied)

I can see that it says it was copied, but I thought that already happened in the line above that?
(
*p2 = *p1;		 // value pointed by p2 = value pointed by p1
)

PARTNERS