F
Felipe Ribeiro
Hi everybody.
I'm experiencing some problems when trying to pass a multimensional
array as a function argument.
The following code evaluates a chess position based solely on the
material left on the board:
---------------------------------------------------------------------------------------------
#include <stdio.h>
#define SIZE_BOARD 8
int evaluate_position(char board[][SIZE_BOARD]);
int main(void)
{
char board[][] = {{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}};
printf("Position evaluation = %d\n", evaluate_position(board));
return 0;
}
int evaluate_position(char board[][SIZE_BOARD])
{
int i, j, count_white = 0, count_black = 0;
for (i = 0; i < SIZE_BOARD; i++) {
for (j = 0; j < SIZE_BOARD; j++) {
if ('a' <= board[j] && board[j] <= 'z') {
switch (board[j]) {
case 'q':
count_black += 9;
break;
case 'r':
count_black += 5;
break;
case 'b':
count_black += 3;
break;
case 'n':
count_black += 3;
break;
case 'p':
count_black += 1;
break;
}
} else if ('A' <= board[j] && board[j] <= 'Z') {
switch (board[j]) {
case 'Q':
count_white += 9;
break;
case 'R':
count_white += 5;
break;
case 'B':
count_white += 3;
break;
case 'N':
count_white += 3;
break;
case 'P':
count_white += 1;
break;
}
}
}
}
return count_white - count_black;
}
---------------------------------------------------------------------------------------------
I receive the following output when trying to compile the program with
gcc:
$ gcc -O -Wall -pedantic -ansi -o chess chess.c
chess.c: In function ‘main’:
chess.c:17: error: array type has incomplete element type
chess.c:17: warning: unused variable ‘board’
I just don't undestand why the compiler gives me the error.
If anybody could give me hand I'd really appreciate.
I'm experiencing some problems when trying to pass a multimensional
array as a function argument.
The following code evaluates a chess position based solely on the
material left on the board:
---------------------------------------------------------------------------------------------
#include <stdio.h>
#define SIZE_BOARD 8
int evaluate_position(char board[][SIZE_BOARD]);
int main(void)
{
char board[][] = {{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}};
printf("Position evaluation = %d\n", evaluate_position(board));
return 0;
}
int evaluate_position(char board[][SIZE_BOARD])
{
int i, j, count_white = 0, count_black = 0;
for (i = 0; i < SIZE_BOARD; i++) {
for (j = 0; j < SIZE_BOARD; j++) {
if ('a' <= board[j] && board[j] <= 'z') {
switch (board[j]) {
case 'q':
count_black += 9;
break;
case 'r':
count_black += 5;
break;
case 'b':
count_black += 3;
break;
case 'n':
count_black += 3;
break;
case 'p':
count_black += 1;
break;
}
} else if ('A' <= board[j] && board[j] <= 'Z') {
switch (board[j]) {
case 'Q':
count_white += 9;
break;
case 'R':
count_white += 5;
break;
case 'B':
count_white += 3;
break;
case 'N':
count_white += 3;
break;
case 'P':
count_white += 1;
break;
}
}
}
}
return count_white - count_black;
}
---------------------------------------------------------------------------------------------
I receive the following output when trying to compile the program with
gcc:
$ gcc -O -Wall -pedantic -ansi -o chess chess.c
chess.c: In function ‘main’:
chess.c:17: error: array type has incomplete element type
chess.c:17: warning: unused variable ‘board’
I just don't undestand why the compiler gives me the error.
If anybody could give me hand I'd really appreciate.