Passing multidimensional array as function argument

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. :)
 
J

jameskuyper

Felipe said:
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'},

The element type for 'board' is char[]. That's an incomplete type
(6.7.5.2p4). You are allowed to declare an array that has an unknown
size (6.7.8p3), as long as you provide an initializer list whose
length determines the size (6.7.8p22), but it's a constraint violation
to declare an array whose element type is an incomplete type
(6.7.5.2p1). In simpler terms, what this means is that only the first
dimension of a multidimensional array can be left unspecified. You'll
have to change that to:

char board[][SIZE_BOARD] = ...
 
F

Falcon Kirtaran

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Felipe said:
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'},

The element type for 'board' is char[]. That's an incomplete type
(6.7.5.2p4). You are allowed to declare an array that has an unknown
size (6.7.8p3), as long as you provide an initializer list whose
length determines the size (6.7.8p22), but it's a constraint violation
to declare an array whose element type is an incomplete type
(6.7.5.2p1). In simpler terms, what this means is that only the first
dimension of a multidimensional array can be left unspecified. You'll
have to change that to:

char board[][SIZE_BOARD] = ...

In my experience one could also declare char** board = ...

- --
- --Falcon Darkstar Christopher Momot
- --
- --OpenPGP: (7902:4457) 9282:A431

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkoJ0/0ACgkQeQJEV5KCpDGW/ACfdTqbhMSCoJNNxfgfuX6z3eqN
cZwAn3P6hk9ycSUC1VAd3soEfCZxq3hw
=/pLe
-----END PGP SIGNATURE-----
 
J

jameskuyper

Falcon said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Felipe said:
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'},

The element type for 'board' is char[]. That's an incomplete type
(6.7.5.2p4). You are allowed to declare an array that has an unknown
size (6.7.8p3), as long as you provide an initializer list whose
length determines the size (6.7.8p22), but it's a constraint violation
to declare an array whose element type is an incomplete type
(6.7.5.2p1). In simpler terms, what this means is that only the first
dimension of a multidimensional array can be left unspecified. You'll
have to change that to:

char board[][SIZE_BOARD] = ...

In my experience one could also declare char** board = ...

Yes, but then the meaning of the declaration would be quite different,
though actual use of 'board' would be pretty much the same. With that
declaration, "board" is a pointer to a pointer to char, so space would
have to be set aside separately to store the pointer it points at, and
space would also have to be set aside to store the char that the
pointer points at.. and it would have to be initialized quite
differently:

char row1[] = {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'};
/* similarly for each of the other 7 rows */
char *rows[] = {row1, row2, row3, row4, row5, row6, row7,
row8};
char **board = &rows;

In C99, there's an alternative way to do the same thing using compound
literals, which avoids the need to name row1 through row8. They would
still exist, they just wouldn't have names. Still, it seems like an
awfully complicated way to do something that can be done much more
simply:

char board[][SIZE_BOARD] = {
"rnbqkbnr",
"pppppppp",
"........",
"........",
"........",
"........",
"PPPPPPPP",
"RNBQKBNR"
};
 
F

Felipe Ribeiro

Felipe said:
#define SIZE_BOARD 8
int evaluate_position(char board[][SIZE_BOARD]);
int main(void)
{
   char board[][] = {{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},

The element type for 'board' is char[]. That's an incomplete type
(6.7.5.2p4). You are allowed to declare an array that has an unknown
size (6.7.8p3), as long as you provide an initializer list whose
length determines the size (6.7.8p22), but it's a constraint violation
to declare an array whose element type is an incomplete type
(6.7.5.2p1). In simpler terms, what this means is that only the first
dimension of a multidimensional array can be left unspecified. You'll
have to change that to:

      char board[][SIZE_BOARD] = ...

Didn't no about it. Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top