Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Passing multidimensional array as function argument

Reply
Thread Tools

Passing multidimensional array as function argument

 
 
Felipe Ribeiro
Guest
Posts: n/a
 
      05-12-2009
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[i][j] && board[i][j] <= 'z') {
switch (board[i][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[i][j] && board[i][j] <= 'Z') {
switch (board[i][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.
 
Reply With Quote
 
 
 
 
jameskuyper
Guest
Posts: n/a
 
      05-12-2009
Felipe Ribeiro wrote:
> 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] = ...
 
Reply With Quote
 
 
 
 
Falcon Kirtaran
Guest
Posts: n/a
 
      05-12-2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jameskuyper wrote:
> Felipe Ribeiro wrote:
>> 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-----
 
Reply With Quote
 
jameskuyper
Guest
Posts: n/a
 
      05-12-2009
Falcon Kirtaran wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> jameskuyper wrote:
> > Felipe Ribeiro wrote:
> >> 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"
};
 
Reply With Quote
 
Felipe Ribeiro
Guest
Posts: n/a
 
      05-13-2009
On May 12, 2:57*pm, jameskuyper <(E-Mail Removed)> wrote:
> Felipe Ribeiro wrote:
> > #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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional array, passing to a function Michael C Programming 16 11-30-2009 08:07 AM
constant multidimensional array in a function argument Szabolcs Borsanyi C Programming 1 04-21-2008 01:49 PM
creating multidimensional array at runtime and passing it as parameter to a function nitinm C Programming 2 09-14-2007 09:30 PM
Passing a function as an argument and using the evaluated functionas an argument User1014 Javascript 1 11-30-2006 12:13 PM
multidimensional array as argument Huub Java 6 05-29-2004 07:58 PM



Advertisments