Can we compare two structures using "==" operator | Sololearn: Learn to code for FREE!


Can we compare two structures using "==" operator

If yes then why and if no then why not

2/14/2019 5:42:46 AM

Kailash Loncha

16 Answers

New Answer


Hi Terminator you're back! (I'm so thrilled!) Well, to answer your question, each struct created is an arrow that references to a particular object. Therefore, you're seeing if the first arrow references the exact same struct that the 2nd struct is referencing.


Haha Serena Yvonne! I probably changed my name like 3 times.


Let's bring this discussion into private chat discussion :)


I wouldn't mind if you asked :) Btw the times have changed so I doubt we should be speaking here.


Terminator No you can't compare structures using "==" operator. You will get the error "invalid operands to binary operator" You need to compare them field by field. "==" operator is a boolean operator so it needs lhs and rhs either as bool or a datatype convertible to bool. structure is a compound datatype hence not convertible to bool(or BOOL)


Cannot compare structures like that but you can compare pointers to structures to see if they refer to the same structure.


⚡Prometheus ⚡ good to see you again brother


⚡Prometheus ⚡ what is your old username


$Vengat Terminator Have I answered your question btw?


Serena Yvonne THANKS


Well yes we can because the "==" operator either makes the first variable/structure equals to the next variable/structure what I mean is if u put it in an if statement it checks if the two structures/variables are the same then it runs the if statement


Yeah. It is relational operator. Relational operators are only used to compare two or more things. I think you got the answer.


Actually yes, but the == operator will only check if the two references of structs point to the same struct.


May be "equals"?