有人请向我解释如何正确使用strcmp
函数?我创建一个井字棋游戏,我不断收到错误:正确使用strcmp函数?
passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
我创建了两个指针是充当strcmp
功能参数。一个是玩家投入的输入,第二个是玩家选择的移动。但是,当我尝试运行代码时,出现上述错误。下面是一段我的代码:
void mark_location(int userU, char str) {
char *moves[] = {"upperLeft", "up", "upperRight", "left", "center", "right", "lowerLeft", "down", "lowerRight"};
if (strcmp(str, moves[0]) == 0)
board[0][0] = userU;
else if (strcmp(str, moves[1]) == 0)
board[0][1] = userU;
else if (strcmp(str, moves[2]) == 0)
board[0][2] = userU;
else if (strcmp(str, moves[3]) == 0)
board[1][0] = userU;
else if (strcmp(str, moves[4]) == 0)
board[1][1] = userU;
else if (strcmp(str, moves[5]) == 0)
board[1][2] = userU;
else if (strcmp(str, moves[6]) == 0)
board[2][0] = userU;
else if (strcmp(str, moves[7]) == 0)
board[2][1] = userU;
else if (strcmp(str, moves[8]) == 0)
board [2][2] = userU;
}
'mark_location(int userU,char * str)' – wildplasser
错误与您的类型有关。但是,在决定选择哪种移动方式时(例如,如果选择了无效移动会发生什么情况),您会有一些问题。你认为你可以把字符串变成一个枚举,然后运行一个switch语句吗? –