简体   繁体   English

C 如何解析char数组中的int和char?

[英]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.我想在我的char数组中解析charint并在代码中使用它们。 For example the char array is a3.例如 char 数组是 a3。 I am getting a warning: "comparison between pointer and integer."我收到一条警告:“指针与 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. char文字由单引号 ( ' ) 而不是双引号 ( " ) 表示,因此您应该检查piece[0] != 'a'等。

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.左操作数的类型为char ,而右操作数的类型为char * ,因为"a"是字符串文字。 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语句中的条件

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您需要使用逻辑 AND 运算符而不是逻辑 OR 运算符,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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