0

I have this code, which is simulating Conway's game(not really important what that is for this question), and I was just wondering how to pass my array, board, to my function neighbor. I keep getting a compiler error saying that I have an incomplete element type in the function declaration, even though I was told I needed to put the empty square brackets there to pass functions, and it doesn't work without the square brackets. If anyone could help here to pass the function, that would be very helpful. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct info
{
    int red;
    int blue;
};

struct info neighbor(char board[][], int, int);

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("usage: dimensions repititions\n");
        return 1;
    }
    srand(time(NULL));
    int games = 0;
    int d = atoi(argv[0]);
    int repititions = atoi(argv[1]);
    //create board
    char board [d][d];
    for(int i = 0; i < d; i++)
    {
        for(int j = 0; j < d; j++)
        {
            int choice = rand() % 100;
            if(choice < 44)
            {
                if(choice % 2 == 0)
                {
                    board[i][j] = '#';
                }
                else
                {
                    board[i][j] = '*';
                }
            }
            else
            {
                board[i][j] = '.';
            }
        }
    }
    while(games < repititions)
    {
        for(int i = 0; i < d; i++)
        {
            for(int j = 0; j < d; j++)
            {
                neighbor(board, i, j);
            }
        }
    }

}

struct info neighbor(char board, int i, int j)
{
    char grid [3][3];
    for(int u = 0; u < 3; u++)
    {
        for(int v = 0; v < 3; v++)
        {
            grid[u][v] = board[i][j];
            i ++;
        }
        j++;
    }
}
2
  • 1
    You have char board[][] in the declaration but char board in the definition. Commented Feb 10, 2020 at 0:27
  • struct info neighbor(char board, int i, int j) => struct info neighbor(char *board, int i, int j) or struct info neighbor(char board[][], int i, int j) it doesn't matter much because array's decay to pointers when called in functions like this. Commented Feb 10, 2020 at 2:05

2 Answers 2

1

When passing a multidimensional array to a function, the size of all dimensions except the first must be specified. When such an array is variable length, the declaration should look like this:

struct info neighbor(int, int, char board[*][*]);

And the definition would look like this:

struct info neighbor(int i, int j, char board[i][j]) {
Sign up to request clarification or add additional context in comments.

2 Comments

how can I pass the entire array, not just one value in the array?
@henryfoster03 This does pass the entire array. char board[i][j] is a declaration, not an expression.
0

In that case you need to pass the number of columns in function parameter that is initializing 2D array.

struct info neighbor(char board[][number_of_columns], int, int);

struct info neighbor(char board[][number_of_columns], int i, int j)

1 Comment

Well, right now, my array dimensions depend on user input at the command line. Do I have to hard code a specific number for the number of columns, or can I still do it this way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.