我试图在函数中使用递归,为此我必须使用局部变量。编译器在定义局部变量的行中显示错误c141。
int minimax(int board[9], int player) {
int winner;
winner = win(board);
if (winner != 0) return winner*player;
int moveminimax;
moveminimax = -1;
int scoreminimax;
scoreminimax = -2;
int i3;
for (i3= 0; i3 < 9; ++i3) {//For all moves,
if (board[i3] == 0) {//If legal,
board[i3] = player;//Try the move
int thisScore;
thisScore = -minimax(board, player*-1);
if (thisScore > scoreminimax) {
scoreminimax = thisScore;
moveminimax = i3;
}board[i3] = 0;//Reset board after try
}
}
if (moveminimax == -1) return 0;
return scoreminimax;
}
6-3-17 4 01pm.c(116): error C141: syntax error near 'int'
//c(116) is the where int winner is defined
当我在程序开始时全局定义变量时,错误消失了。
最佳答案
我的猜测是,Keil C编译器没有遵循C99标准,在该标准中可以在任何地方定义变量,而是遵循较旧的C89标准,其中只能在块的开头定义局部变量。
这意味着像
int winner;
winner = win(board);
if (winner != 0) return winner*player;
int moveminimax;
moveminimax = -1;
int scoreminimax;
scoreminimax = -2;
int i3;
无效,因为它包含混合的声明和语句。
声明变量时,可以通过初始化变量来完全删除其中的两个语句,从而无需调用函数调用和
if
语句。尝试以下方法:
int winner;
int moveminimax = -1;
int scoreminimax = -2;
int i3;
winner = win(board);
if (winner != 0) return winner*player;
关于c - C编程AT89C51中的局部变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44343374/