Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get this error? invalid operands to binary expression

I want to check if 2 struct pointers point to the same values, but I get this error:

./07ex.c:158:15: error: invalid operands to binary expression (‘RGB’ (aka ‘struct RGB_’) and ‘RGB’)
return *x == *y;
Here is the struct and the function:

typedef struct RGB_ {
    float r; 
    float g; 
    float b; 
} RGB;

bool point_to_equal_values_struct(RGB *x, RGB *y) {
    return *x == *y;
}

I got through this by doing it as such:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

bool point_to_equal_values_struct(RGB *x, RGB *y) {
    if ((*x).r == (*y).r && (*x).g == (*y).g && (*x).b == (*y).b)
    {
        return true;
    }       
    return false;
}

But still, I am curious as to why the compiler gives out this error.
Thanks in advance!

>Solution :

The == operator isn’t defined to work on structs, so two structs can’t be compared directly.

You need to compare the corresponding fields as you did. On a side node, you probably want to use the -> operator to access members of a struct referenced by a pointer as it’s easier to read:

if (x->r == y->r && x->g == y->g && x->b == y->b)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading