What is the difference between *variable and *variable-'0'?

Asked

Viewed 132 times

1

I’m writing a Sudoku game with C and ncurses, below follows the code of a function that receives a pointer to a variable (ch) which stores typed key with function getch(), if the number already exists horizontally, vertically or in the block returns false, if not true.

Doubt:

  • In the code, if you replace *ch-'0' for *ch, it stops working correctly, returns false only in numbers added by user, ignoring the standards, the behavior also changes according to the cursor position. Why? What is the difference?

Code:

bool
num(int *ch)
{
    // g.y e g.x Armazenam a posição do cursor no array
    // g.r_board Array de referência
    if (g.r_board[g.y][g.x] > 0)
    {
        hide_banner(); // Esconde frase da tela
        show_banner("Número fixo"); // Mostra uma frase na tela
        show_cursor(); // Move o cursor pra posição original
        return false;
    }

    // Verifica se tem número igual na horizontal
    // g.board Array que armazena números do jogo
    for (int i = 0; i < 9; i++)
        if (g.board[g.y][i] == (*ch-'0'))
        {
            hide_banner();
            show_banner("Número igual horizontal");
            show_cursor();
            return false;
        }

    // Verifica se tem número igual na vertical
    for (int i = 0; i < 9; i++)
        if (g.board[i][g.x] == (*ch-'0'))
        {
            hide_banner();
            show_banner("Número igual vertical");
            show_cursor();
            return false;
        }

    // Verifica se tem número igual dentro do bloco
    for (int i = (g.y/3)*3, j = i+3; i < j; i++)
        for (int k = (g.x/3)*3, l = k+3; k < l; k++)
            if (g.board[i][k] == (*ch-'0'))
            {
                hide_banner();
                show_banner("Número igual bloco");
                show_cursor();
                return false;
            }

    return true;
}

1 answer

3


This is the same thing as doing

*ch - 48

I put in the Github for future reference.

already '0' is a character displaying text of a zero and equivalent to code 48 in decimal of ascii table. He’s not worth 0 as you might think at first. He used the type char because it is more semantic for this code, but arithmetic is defined in binary, no matter if it has a number a character in its source code.

If the code asks for a subtraction and you remove the result will go wrong.

It’s taking the received code and subtracting the character from zero to make the value reset, that is if you typed the character 0, you want the number 0, then if the character 0 is worth 48, you have to subtract 48 to reach 0, which is what that expression does most simply, you don’t even need to know that the character 0 is worth 48 in the ASCII table. The same goes for the other characters, if you type the character 1, of course it will take the code 49, minus 48 of the character 0 that is there in the expression and will give the number 1.

  • I arrived at this code on trial-error basis, I wrote several. The curious thing is that if you write *ch - 0, *chor anything other than *ch-'0' does not work, do not understand why, does not make sense, reading all the code does not make sense even, *ch shouldn’t it work? I mean, the whole value within the variable is being compared to the integer values of the array.

  • If you do 30 - 20 and wants the result 10, if you do only 30 will result in 10?

  • 1

    Got it, the function getch() stores the whole number of the ascii table and not the literal number, for example, digit 1 and the function stores 49 in the variable. The documents I read about this function didn’t say that, so I let it go unnoticed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.