How can Can I copy the contents of one variable into another???

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 3
Reputation: Farisnet is an unknown quantity at this point 
Solved Threads: 0
Farisnet Farisnet is offline Offline
Newbie Poster

How can Can I copy the contents of one variable into another???

 
0
  #1
Apr 15th, 2008
Hi...


I am a bit new to programming, and I'm trying to learn C++. I was wondering if there was a way to copy the contents of one variable into another... So that, for example:


 
int x=5;
int y;
(Some fancy code here involving x and y);
(y=5 but it is not related at all to x. If x is assigned a value of 10438478234 later in the code, y will still equal 5)

The reason is that if I use variable assingment or pointers, and x changes, then so will y... If you please, list a code or method in order to achieve this data transfer

Thank you,
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How can Can I copy the contents of one variable into another???

 
0
  #2
Apr 15th, 2008
int *y = &x; If x changes, then will *y
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: How can Can I copy the contents of one variable into another???

 
0
  #3
Apr 15th, 2008
If you want 2 variables to refer to the same data, you can also use references:

  1. int x = 5;
  2. int &y = x;
  3.  
  4. cout << x << "," << y << endl;
  5. x = 10;
  6. cout << x << "," << y << endl;
  7. y = 15;
  8. cout << x << "," << y << endl;
output:
5,5
10,10
15,15
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 3
Reputation: Farisnet is an unknown quantity at this point 
Solved Threads: 0
Farisnet Farisnet is offline Offline
Newbie Poster

Re: How can Can I copy the contents of one variable into another???

 
0
  #4
Apr 16th, 2008
You missed my point. What I want is that if I change x, y is not affected.

Thank you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,847
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How can Can I copy the contents of one variable into another???

 
0
  #5
Apr 16th, 2008
Ok...
  1. int x =3;
  2. int y= 2;
  3. cout << "x: " << x << " y: " << y << endl;
  4. y=x;
  5. cout << "x: " << x << " y: " << y << endl;
  6. x=10;
  7. cout << "x: " << x << " y: " << y << endl;
output:
  1. x: 3 y: 2
  2. x: 3 y: 3
  3. x: 10 y: 3
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 3
Reputation: Farisnet is an unknown quantity at this point 
Solved Threads: 0
Farisnet Farisnet is offline Offline
Newbie Poster

Re: How can Can I copy the contents of one variable into another???

 
0
  #6
Apr 16th, 2008
It does work like that?? But what about the "x=y" part, followed by "x=10"???? Doesn't that say "x has same value as y, and x is now 10" Doesn't that mean that y will also be 10?? (Please excuse my noobish questions, I'm just staring with this thing!!!)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: How can Can I copy the contents of one variable into another???

 
0
  #7
Apr 17th, 2008
You sound like a mathematician, not a programmer.

In programming, x = 5 means set x to 5. y = x means set x to the value of y (the old value of x is discarded).
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: Yellowdog428 is an unknown quantity at this point 
Solved Threads: 0
Yellowdog428 Yellowdog428 is offline Offline
Newbie Poster

Re: How can Can I copy the contents of one variable into another???

 
0
  #8
Apr 17th, 2008
er...

look at it this way

when there is a = sign remember that it is an assignment operator not an "equal" sign

anything on the left is assigned to what is on the right

But what about the "x=y" part, followed by "x=10"???? Doesn't that say "x has same value as y, and x is now 10" Doesn't that mean that y will also be 10??
Looks like you read it wrong.
the only variable assignment he made was y = x

cheers
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: How can Can I copy the contents of one variable into another???

 
1
  #9
Apr 17th, 2008
Originally Posted by dougy83 View Post
You sound like a mathematician, not a programmer.

In programming, x = 5 means set x to 5. y = x means set x to the value of y (the old value of x is discarded).
That should have been x = y. The complexity of the statement must have confounded me..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC