Thread: Passing a 2d array help?

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    5

    Passing a 2d array help?

    Hello all,

    I'm trying to print a tic tac toe board which needs the 2d array of user inputs. I keep getting "an array may not have elements of this type" under the braces of the array when I try to do the following:

    Code:
    char board[3][3] = { {' ',' ',' '},{' ',' ',' '},{' ',' ',' '} };
    void printBoard(char board[][]);
    which is trying to pass the array board to:

    Code:
    void printBoard(char board[][]){
        printf(" %c | %c | &c \n", board[0][0], board[0][1], board[0][2]);
        printf("---|---|---\n");
        printf(" %c | %c | &c \n", board[1][0], board[1][1], board[1][2]);
        printf("---|---|---\n");
        printf(" %c | %c | &c \n\n", board[2][0], board[2][1], board[2][2]);
    }
    The assignment also said I could use a pointer

    Code:
    char *ptr = &board[0][0];
    and I saw some uses of it on other websites, but I am unsure how to implement it other than checking which character ('X' or 'O') would be in whatever part of the array it's pointing to.

    The prompt doesn't specify that I need to use another function for the game at all, but I would prefer to have neater code using multiple functions rather than write everything in main.

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void printBoard(char board[][]);
    You have to specify the sizes of all the minor dimensions.

    void printBoard(char board[][3]);

    Other than that, the body of printBoard looks good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    5
    Thanks! Worked like a charm!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  2. Passing an Array
    By Jpeg6 in forum C Programming
    Replies: 5
    Last Post: 11-07-2010, 09:34 AM
  3. Passing array right
    By devilsknight in forum C Programming
    Replies: 7
    Last Post: 06-18-2006, 12:21 AM
  4. array passing
    By manwhoonlyeats in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2003, 12:51 PM
  5. passing an array to a function (2d array)
    By revelation437 in forum C Programming
    Replies: 5
    Last Post: 03-11-2003, 03:32 PM

Tags for this Thread