-3

I am trying to use recursion in a function and for that I have to use local variables. The compiler gives error c141 in the line where I am defining my local variable.

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

When i define my variables globally in the beginning of program the error goes away.

  • 1
    Local variables in C are declared and defined the same everywhere. What is the *actual* problem you have? Do you get build errors for the code you have? Something else? Can you please elaborate. – Some programmer dude Jun 03 '17 at 11:24
  • @Someprogrammerdude It gives error when i define local variables – AsksBadQuestions Jun 03 '17 at 11:25
  • 1
    Then better ask about *that* instead! Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jun 03 '17 at 11:26
  • 1
    Wouldn't it make sense to include the code and maybe at least a reference to the error codes? How do you expect anyone to help? Be explicit about which compiler you're using. – Dave Newton Jun 03 '17 at 11:36
  • For example, in five seconds I found docs for the two errors you mention for one of their compilers. But it's impossible to tell if it's related because all you said was that you got this error number for code you didn't post. `¯\_(ツ)_/¯` – Dave Newton Jun 03 '17 at 11:40
  • An 89C51 has exactly 128 bytes of RAM. To me, the program doesn't look much as if it had been written with that in mind. – tofro Jun 03 '17 at 11:51
  • LOL, 8051 and 'recursion' in the same post:) – ThingyWotsit Jun 03 '17 at 14:40
  • Recursion on an 8051 is brave (or foolish); especially with a recursive function this complex. Is the depth of recursion strictly bounded? What is the worst case depth? Do you have sufficient stack for that?. Without knowing the compiler implementation and calling convention details I would estimate that this function requires a stack frame of at least 14 bytes; and on 8051 stack is a very limited resource. – Clifford Jun 03 '17 at 16:13

1 Answers1

3

My guess is that the Keil C compiler is not following the C99 standard, where variables could be defined anywhere, but instead follow the older C89 standard where local variables only could be defined at the beginning of block.

That means code like

int winner;
winner  = win(board);
if (winner != 0) return winner*player;

int moveminimax;
moveminimax = -1;
int scoreminimax;
scoreminimax = -2; 
int i3;

is invalid since it contains mixed declarations and statements.

Two of the statements can be removed completely by initializing the variables when you declare them, which leaves the function call and if statement that needs to be moved.

Try this instead:

int winner;
int moveminimax = -1;
int scoreminimax = -2;
int i3;

winner  = win(board);
if (winner != 0) return winner*player;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @Notanengineer : It is great that it works, but in the question you stated that line 116 corresponds to the line where `winner` is declared, which was a valid declaration - the error should have been indicated at the `moviminmax` declaration. – Clifford Jun 03 '17 at 16:03
  • 1
    Note also that you can still initialise a variable at its declaration; `int winner = win( board ) ;` is valid, rather then separate declaration and assignment.. – Clifford Jun 03 '17 at 16:05