for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        ****RGBTRIPLE allred;

        RGBTRIPLE changedpixel;

        if (triple==allred)

        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

I dont understand why whodunit.c doesnt compile. I keep getting the error invalid operands to binary expression. The variables in my conditional statement seems to be of same data type so i dont know what is wrong.


According to this article from c-faq.com:

There is not a good way for a compiler to implement structure comparison (i.e. to support the == operator for structures) which is consistent with C's low-level flavor. A simple byte-by-byte comparison could founder on random bits present in unused ``holes'' in the structure (such padding is used to keep the alignment of later fields correct; see question 2.12).

An internet search for "compare structs in c" yields lots of results, all saying the same thing. They all agree that structs need to be compared element by element.

  • Is this because what the == operator does is to compare every element so specifically, including their size? – Shane Jul 27 '17 at 11:15
  • I think what the article says is that == does not compare every element of the struct, and that is the proper way to compare structs. Therefore the compiler doesn't allow it (ie struct1 == struct2), so any comparison is left up to the programmer to compare each element. – DinoCoderSaurus Jul 27 '17 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .