| | |
How can Can I copy the contents of one variable into another???
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
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:
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,
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
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
If you want 2 variables to refer to the same data, you can also use references:
output:
5,5
10,10
15,15
CPP Syntax (Toggle Plain Text)
int x = 5; int &y = x; cout << x << "," << y << endl; x = 10; cout << x << "," << y << endl; y = 15; cout << x << "," << y << endl;
5,5
10,10
15,15
Ok...
output:
c Syntax (Toggle Plain Text)
int x =3; int y= 2; cout << "x: " << x << " y: " << y << endl; y=x; cout << "x: " << x << " y: " << y << endl; x=10; cout << "x: " << x << " y: " << y << endl;
C++ Syntax (Toggle Plain Text)
x: 3 y: 2 x: 3 y: 3 x: 10 y: 3
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
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
Looks like you read it wrong.
the only variable assignment he made was y = x
cheers
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??
the only variable assignment he made was y = x
cheers
![]() |
Other Threads in the C++ Forum
- Previous Thread: '=' : left operand must be l-value
- Next Thread: binary tree
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






