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

+6

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

+7

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.

+8

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

+6

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

+6

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

+5

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)

+5

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

+4

⚡Prometheus ⚡ good to see you again brother

+3

⚡Prometheus ⚡ what is your old username

+3

$Vengat Terminator Have I answered your question btw?

+3

Serena Yvonne THANKS

+2

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

+1

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

0

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

-1

May be "equals"?