3

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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
LunaAquila
  • 45
  • 4
  • 2
    Use single quotes so that you have a char and not char* that can be used in comparasions with int so change `bool isValid(char piece[1])` to `bool isValid(char *piece)` then single quotes `piece[0] != 'a'` and so on. – Jack Dec 24 '20 at 23:10
  • @Hilal Turfullu What does the function check? – Vlad from Moscow Dec 24 '20 at 23:22
  • @VladfromMoscow checks if there is an input out of the board column/row. The board has column 1 to 7 and row a to g. – LunaAquila Dec 25 '20 at 10:07

2 Answers2

1

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

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

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...
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Use && in if statement differences from situation to situation. && can't give me what i want. But (piece[0] != 'a') solved my problem. Thank you! – LunaAquila Dec 25 '20 at 10:17