简体   繁体   中英

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM