vendredi 22 décembre 2017

Should this go through "if" or not? (C language)

To put it short, the problem is that it's passing through an IF statement when the argument is False.

Questions: is the argument actually false? I am talking about the 2nd "if" from the block below. I used many "or" operand, that's what I am afraid of.

Please, I couldn't sleep last night because of this bug.

if(iNou == (height - 1) ) 
    {
        if(ptMatricePixeliB[iNou][jNou - 1] == 'X' || ptMatricePixeliB[iNou - 1][jNou - 1] == 'X' || ptMatricePixeliB[iNou - 1][jNou] || ptMatricePixeliB[iNou - 1][jNou + 1] == 'X' || ptMatricePixeliB[iNou][jNou + 1] == 'X') 
        {
            return 0; 
        }else 
            {
                return 1; 
            }
    }  

When I used the debugger I got this:

if(ptMatricePixeliB[iNou][jNou - 1] == 'X' || ptMatricePixeliB[iNou - 1][jNou - 1] == 'X' || ptMatricePixeliB[iNou - 1][jNou] || ptMatricePixeliB[iNou - 1][jNou + 1] == 'X' || ptMatricePixeliB[iNou][jNou + 1] == 'X') 
(gdb) s
837             return 0; 
(gdb) p ptMatricePixeliB[iNou][jNou - 1]
$8 = 32 ' '
(gdb) p ptMatricePixeliB[iNou-1][jNou - 1]
$9 = 32 ' '
(gdb) p ptMatricePixeliB[iNou-1][jNou]
$10 = 32 ' '
(gdb) p ptMatricePixeliB[iNou-1][jNou + 1]
$11 = 32 ' '
(gdb) p ptMatricePixeliB[iNou][jNou + 1]
$12 = 32 ' '

Aucun commentaire:

Enregistrer un commentaire