Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written this code to test something for some code I am writing and it only works if I use == rather than =:

#include <iostream>
using namespace std;
int main ()
{
int internal_organ_cream = 5;
int liquid_organs = 7;
&internal_organ_cream = &liquid_organs;
}

I tried running it like this but it didnt work but when I changed the = to a == it worked. I dont understand why because i thought == was for when you are checking not when you are assigning because here i am trying to make internal_organ_cream a reference of liquid_organs.

What I have tried:

nothing....................................................................
Posted
Updated 16-Mar-18 5:11am

1 solution

It's because of the "&" in front of internal_organ_cream - remove them both and it will work:
C++
int internal_organ_cream = 5;
int liquid_organs = 7;
internal_organ_cream = liquid_organs;
When you put "&" in front of a variable name, it returns the address of the variable, rather than it's content - but that will be explained later when you get to pointers. For the moment, just don't use them at all!
 
Share this answer
 
Comments
BerthaDusStuf 16-Mar-18 13:59pm    
Hi thanks for your reply, yes i do know what the & does and i also have a fair bit of knoledge on pointers but i still dont know why there is a double equals there rather than single. is that something ill learn about or is there just a simple explanation for it? also i dont want to do it without the ampersand because i want it to have the same backing memory rather than make a copy
OriginalGriff 16-Mar-18 15:06pm    
Please don't play with silly comments - you won't get near the maximum length, but you will annoy people whose browser width goes "all funny" as a result ... I delete it for you to prevent you getting tagged as a "abusive / trolling".

You don't need "==" to do the assignment - the reason it wanted it instead of a single "=" was that the & before the first variable changed it from an lvalue to an rvalue.
An lvalue can appear on the left of an assignment, and rvalue can't - the different being that an lvalue can be assigned to, or changed where an rvalue can't.
Lvalues are variables like "i", dereferenced pointers like "*pi", array elements like arr[10], and so forth.
Rvalues are constants like 666 , or calculated values line "i * 2". And the addresses of variables of course, like "&i".

When you added the ampersand, you changed "internal_organ_cream" from a lvalue to an rvalue, so the compiler assumed that you can mistyped the assignment "=" and intended a comparison "==" and gave you an appropriate error / warning message.
BerthaDusStuf 16-Mar-18 20:33pm    
oh ok but then I tried running that program and it changed the value of it so it didnt compare, it actually assigned it that value
OriginalGriff 17-Mar-18 3:16am    
Which is what you were wanting it to do ... wasn't it?
OriginalGriff 16-Mar-18 15:08pm    
And the long message? That also annoy people who get a 4K line of "d"s delivered to their inbox ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900