有人請向我解釋如何正確使用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語句嗎? –