【发布时间】:2012-01-22 19:04:41
【问题描述】:
有人可以向我解释如何正确使用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) -
错误与您的类型有关。但是,您在选择选择哪一招时存在一些问题(例如,如果选择了无效的招,会发生什么?)。您认为您可以将字符串转换为枚举然后运行 switch 语句吗?