Home > OS >  "Invalid operands to binary expression"
"Invalid operands to binary expression"

Time:11-09

in my first year University class we have just started fiddling with arrays and on the worksheet I was given this code, which doesn't seem to work. I've been scanning and looking for a problem but nothing seems to fix it. Here is my code...

#include <stdio.h>
#include <stdbool.h>

int main(){

    int size = 10;
    float suspect[size]; //Declaring suspect array
    
    int sizeR = 3;
    int sizeC = 10;
    float criminals[sizeR][sizeC]; //Declaring criminals array

    //Read 10 input values into suspect array from keyboard
    printf("Enter the 10 chromosomes of the suspect separated by spaces: \n");
    for (int i = 0; i < size; i  )
        scanf(" %f", &suspect[i]);

    //Read multiple profiles of 10 values into criminals array from the keyboard
    for (int i = 0; i < sizeR; i  ){
        printf("Enter the 10 chromosomes of the %dth criminal: \n", i 1);

    //Read 10 input values of a criminal into criminals array from the keyboard
    for (int j = 0; j < sizeC; j  )
        scanf(" %f", &criminals[i][j]);

    }

    //Match two profiles
    bool match = true;
    for (int i = 0; i < size; i  )
        if(suspect[i] != criminals[i]) //Error is in this line
            match = false;

    //Display matching result
    if (match)
        printf("The two profiles match! \n");
    else
        printf("The two profiles don't match! \n");

    return 0;
}

And when I run this code, I am returned with:

error: invalid operands to binary expression ('float' and 'float [sizeC]')

With the error being pointed to the != in the matching two profiles part. Excuse me if the solution is simple, coding is relatively new to me and I am struggling to find the solution to this particular problem using Google.

CodePudding user response:

In this if statement

if(suspect[i] != criminals[i]) //Error is in this line

the expression criminals[i] is implicitly converted to the type float * because the original type of the expression before the conversion is float[sizeC].

And moreover the expressions suspect[i] has the type float. That is there are compared an object of the type float with a pointer of the type float * that does not make a sense.

So the compiler issues the error message.

If you are going to compare the array suspect with elements of the two-dimensional array criminals you should use one more inner for loop.

  • Related