调试器跳过整个 if 语句

Debugger skipping a whole if statement

/**
 * Copies a BMP piece by piece, just because.
 * All we have to do is change all the red pixels to 


 */

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 3)
    {
        fprintf(stderr, "Usage: ./copy infile outfile\n");
        return 1;
    }

    // remember filenames
    char *infile = argv[1];
    char *outfile = argv[2];

    // open input file 
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // write outfile's BITMAPFILEHEADER
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    // determine padding for scanlines
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // iterate over infile's scanlines
    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 red = {0, 0, 255};
            int same = 0; 
            RGBTRIPLE white = {255, 255, 255};

            if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
            {
                same = 1; 
            }

            if(same == 0) //copy the pixel, if the pixel isn't red
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);    
            }
            if(same == 1)
            {
                fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
            }

        }

        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);

        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

我的问题更具体地与这一段代码有关:

// temporary storage
            RGBTRIPLE triple;

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

            RGBTRIPLE red = {0, 0, 255};
            int same = 0; 
            RGBTRIPLE white = {255, 255, 255};

            if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
            {
                same = 1; 
            }

            if(same == 0) //copy the pixel, if the pixel isn't red
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);    
            }
            if(same == 1)
            {
                fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
            }

我正在尝试将一个 bmp 文件复制到另一个文件,但去掉任何红色并将其替换为白色(这段代码的大部分不是我的工作,它是我应该为赋值),复制功能有效,但由于某种原因检测红色并将其替换为白色无效。奇怪的是,当我使用调试器时,它完全跳过了这部分代码:

 if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
        {
            same = 1; 
        }

这是我检测像素是否为红色的地方,调试器的行为就好像该行不存在一样。

不要比较地址,它总是会失败。比较值,因为这会导致简单的整数比较

if(red.rgbtRed == triple.rgbtRed && red.rgbtGreen != triple.rgbtRed && red.rgbtBlue != triple.rgbtRed) {
    same = 1; 
}

不要比较地址,您根本不需要使用 int same 变量。

if(red.rgbtRed == triple.rgbtRed && red.rgbtGreen != triple.rgbtRed && red.rgbtBlue != triple.rgbtRed) 
{
    fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
}
else
{
    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}

您的编译器可能会检测到此 "if" 条件始终为假,并优化这些行。因此,这部分代码不存在于您的可执行文件中,您的调试器也看不到它。