Search code examples
arraysccharcompiler-warningslogical-and

C How to parse int and char in char array?


I want to parse char and int in my char array and use them in the code. For example the char array is a3. I am getting a warning: "comparison between pointer and integer." How can I fix this?

bool isValid(char piece[1]){
    if((piece[0] != "a") || (piece[0] != "b") || (piece[0] != "c") || (piece[0] != "d") || (piece[0] != "e") || (piece[0] != "f") || (piece[0] != "g") || (piece[1] <= 0) || (piece[1] > 7))
        return false;
    else
        return true;

Solution

  • For starters in expressions like this

    (piece[0] != "a")
    

    the left operand has the type char while the right operand has the type char * because "a" is a string literal. It seems you are going to compare two characters. So instead of string literals use character constants like

    (piece[0] != 'a')
    

    Secondly, the condition in the if statement

    if((piece[0] != 'a') || (piece[0] != 'b') || and so on...
    

    is incorrect. You need to use the logical AND operator instead of the logical OR operator like

    if((piece[0] != 'a') && (piece[0] != 'b') && and so on...