Passing multidimensional array as function argument

Discussion in 'C Programming' started by Felipe Ribeiro, May 12, 2009.

  1. 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. :)
    Felipe Ribeiro, May 12, 2009
    #1
    1. Advertising

  2. Felipe Ribeiro

    jameskuyper Guest

    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] = ...
    jameskuyper, May 12, 2009
    #2
    1. Advertising

  3. -----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-----
    Falcon Kirtaran, May 12, 2009
    #3
  4. Felipe Ribeiro

    jameskuyper Guest

    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"
    };
    jameskuyper, May 12, 2009
    #4
  5. On May 12, 2:57 pm, jameskuyper <> 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!
    Felipe Ribeiro, May 13, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Huub
    Replies:
    6
    Views:
    596
  2. nitinm
    Replies:
    2
    Views:
    325
    Barry Schwarz
    Sep 14, 2007
  3. Szabolcs Borsanyi

    constant multidimensional array in a function argument

    Szabolcs Borsanyi, Apr 21, 2008, in forum: C Programming
    Replies:
    1
    Views:
    325
    Ben Bacarisse
    Apr 21, 2008
  4. Michael
    Replies:
    16
    Views:
    1,000
    David Thompson
    Nov 30, 2009
  5. User1014
    Replies:
    1
    Views:
    174
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page