C How to parse int and char in char array?

Viewed 117

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;
2 Answers

char literals are denoted by single quotes (') not double quotes ("), so you should check piece[0] != 'a' etc.

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...
Related