Thread: Two Dimensional Arrays

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Two Dimensional Arrays

    I am very new to C and am getting an error and a warning that I am not sure how to fix. I am very prone to problems with declarations and especially the types in declarations so I am fairly sure that is what my problem is. Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define BOARD_SIZE 4
    
    void initial_board (char board [], int board_size);
    
    int
    main ()
    {
    
      char board[BOARD_SIZE][BOARD_SIZE];
      initial_board (board, BOARD_SIZE);
    
      return 0;
    }
    
    void
    initial_board (char board [], int board_size)
    {
      int i, j;
      for (i = 0; i < board_size; i++)
        {
          for (j = 0; j < board_size; j++)
        {
          board[i][j] = '.';
        }
        }
      for (i = 0; i < board_size; i++)
        {
          for (j = 0; j < board_size; j++)
        {
          printf ("%\t", board[i][j]);
        }
          printf ("\n");
        }
    }
    What I get when compiling:

    warning: passing argument 1 of "initial_board" from incompatible pointer type.
    occurs at: initial_board (board, BOARD_SIZE);
    note: expected "char *" but argument is of type "char (*)"
    occurs at: oid initial_board (char board [], int board_size);
    error: subscripted value is neither array nor pointer
    occurs at: board[i][j] = '.';
    error: subscripted value is neither array nor pointer
    occurs at: printf ("%\t", board[i][j]);

    Thanks for any help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why would you think that would work?
    Code:
    void initial_board (char board [], int board_size);
     
    int
    main ()
    {
     
      char board[BOARD_SIZE][BOARD_SIZE];
      initial_board (board, BOARD_SIZE);
    board has 2 dimensions, but your function expects an array that only has one dimension.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Ahh yes, that was quite a silly mistake. However, changing

    Code:
    initial_board (char board[], int board_size)
    to

    Code:
    initial_board (char board[][], int board_size)
    gives me an error of "array type has incomplete element type" even though I want all of the elements of the two dimensional array to be chars.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    C only allows you have have the leftmost set of brackets empty. You need to put the dimension in the right brackets, like char board[][BOARD_SIZE].

  5. #5
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    When passing a multidimensional array in this manner, only the first dimension can be omitted.

    Try:
    Code:
    void initial_board (char board[][BOARD_SIZE], int board_size)
    Also you don't have to pass board_size because you defined it.
    Last edited by rmatze; 10-20-2011 at 04:44 PM. Reason: 30 secs too slow...

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The reason for this is that a, say, 50x30 array is stored in memory as 1,500 consecutive elements. If you pass the array to a function, how can the function know it's a 50x30 array and not 30x50 or 15x100 or 3x500 if you don't tell it what the dimensions are?
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Thanks for clearing that up guys. I fixed it and it makes sense. One other quick question that I had though was in terms of returning multiple values. For this program I want to take in a user's input several times and then use that input to manipulate that spot in the array. So I scanf in x and y values for the player's move each turn but how do I return these values from my scanning function so that I can manipulate them in other functions? I know I can't return multiple values, so should I just use pointers for the x and y values and then manipulate them in other functions through the pointers?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes, you will have to do that with pointers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Three ONE dimensional arrays
    By victory1 in forum C Programming
    Replies: 5
    Last Post: 11-16-2009, 07:14 AM
  2. 2-dimensional arrays
    By rachael033 in forum C++ Programming
    Replies: 7
    Last Post: 06-02-2006, 06:04 AM
  3. Two dimensional arrays
    By Masschino in forum C Programming
    Replies: 9
    Last Post: 05-18-2004, 08:17 PM
  4. dimensional arrays
    By ZakkWylde969 in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2003, 04:49 PM
  5. Help with 2 dimensional arrays
    By riotact in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2002, 10:19 AM