943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1113
  • C++ RSS
Apr 15th, 2008
0

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

Expand Post »
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,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Farisnet is offline Offline
3 posts
since Apr 2008
Apr 15th, 2008
0

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

int *y = &x; If x changes, then will *y
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 15th, 2008
0

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

If you want 2 variables to refer to the same data, you can also use references:

CPP Syntax (Toggle Plain Text)
  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
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Apr 16th, 2008
0

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

You missed my point. What I want is that if I change x, y is not affected.

Thank you!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Farisnet is offline Offline
3 posts
since Apr 2008
Apr 16th, 2008
0

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

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:
C++ Syntax (Toggle Plain Text)
  1. x: 3 y: 2
  2. x: 3 y: 3
  3. x: 10 y: 3
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 16th, 2008
0

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

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!!!)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Farisnet is offline Offline
3 posts
since Apr 2008
Apr 17th, 2008
0

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

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).
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Apr 17th, 2008
0

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

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

Quote ...
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
Reputation Points: 27
Solved Threads: 0
Light Poster
Yellowdog428 is offline Offline
25 posts
since Mar 2008
Apr 17th, 2008
1

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

Click to Expand / Collapse  Quote originally posted by dougy83 ...
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..
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: '=' : left operand must be l-value
Next Thread in C++ Forum Timeline: binary tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC