1

i'm making a game of life in C. I was told to use typedef and enum wherever possible, so I made a typedef state to signal whether a cell is DEAD or ALIVE. I made a 3d array that holds all the boards in all generations. when I try to use the array as a parameter of a function, it says error: array type has incomplete element type. Did I do the typedef wrong? Can you not have arrays of user defined types? Here's my code:

#include <stdio.h>
#include <stdlib.h>
typedef enum { DEAD, ALIVE } state;
void nextGeneration(state[][][], int, int, int);
int numberOfNeighbours(state[][][], int, int, int);
void printGeneration(state[][][], int, int, int);
int main(void)
{
        FILE *filePath;
        int boardHeight;
        int boardWidth;
        int requestedGenerations;
        state board[100][10][10] = { DEAD };
        int h;
        int w;
        if((filePath = fopen("file1", "r")) == NULL)
        {
                fprintf(stderr, "No such file.\n");
                exit(1);
        }
        if(fscanf(filePath, "%d %d %d", &requestedGenerations, &boardHeight, &boardWidth) != 3)
        {
                fprintf(stderr, "File doesn't contain the number of requested generations or the board's size.\n");
                exit(2);
        }
        for (h = 0; h < boardHeight; h++)
                for(w = 0; w < boardWidth; w++)
                        fscanf(filePath, "%d", &board[0][h][w]);
        while(requestedGenerations > 0)
        {
                nextGeneration(board, requestedGenerations--, boardHeight, boardWidth);
                printGeneration(board, requestedGenerations, boardHeight, boardWidth);
        }
}
void nextGeneration(state board[][][], int requestedGeneration, int boardHeight, int boardWidth)
{
        int h;
        int w;
        int currentNumOfNeighbours;
        for(h = 0; h < boardHeight; h++)
                for(w = 0; w < boardHeight; w++)
                {
                        currentNumOfNeighbours = numberOfNeighbours(board, requestedGeneration, h, w);
                        if(board[requestedGeneration][h][w] == ALIVE)
                        {
                                if(currentNumOfNeighbours == 2 || currentNumOfNeighbours == 3)
                                        board[requestedGeneration + 1][h][w] == ALIVE;
                        } else if(currentNumOfNeighbours == 3)
                                board[requestedGeneration + 1][h][w] == ALIVE;
                        }
                }
}
int numberOfNeighbours(state board[][][], int requestedGeneration, int h, int w)
{
        int result = 0;
        if(board[requestedGeneration][h][w + 1]) result++;
        if(board[requestedGeneration][h][w - 1]) result++;
        if(board[requestedGeneration][h + 1][w]) result++;
        if(board[requestedGeneration][h - 1][w]) result++;
        if(board[requestedGeneration][h + 1][w + 1]) result++;
        if(board[requestedGeneration][h - 1][w + 1]) result++;
        if(board[requestedGeneration][h + 1][w - 1]) result++;
        if(board[requestedGeneration][h + 1][w - 1]) result++;
        return result;
}
void printGeneration(state board[][][], int requestedGeneration, int boardHeight, int boardWidth)
{
        int h;
        int w;
        for(h = 0; h < boardHeight; h++)
        {
                for(w = 0; w < boardWidth; w++)
                        printf("%d", board[requestedGeneration][h][w]);
                printf("\n");
        }
}

Actual error messages:

program1.c:4: error: array type has incomplete element type
program1.c:5: error: array type has incomplete element type
program1.c:6: error: array type has incomplete element type
program1.c: In function ‘main’:
program1.c:31: error: type of formal parameter 1 is incomplete
program1.c:32: error: type of formal parameter 1 is incomplete
program1.c: At top level:
program1.c:35: error: array type has incomplete element type
program1.c: In function ‘nextGeneration’:
program1.c:43: error: type of formal parameter 1 is incomplete
program1.c: At top level:
program1.c:52: error: array type has incomplete element type
program1.c:65: error: array type has incomplete element type

Any help would be much appreciated, thanks :).

2
  • Wait, are you using C99 variable-length arrays or something? Because otherwise I don't think a function can accept a multi-dimensional array parameter with any "unknown" dimension other than the last. Commented Apr 18, 2014 at 10:38
  • @Medinoc So you're saying I should do something like f(state[requestedGenerations][boardHeight][boardWidth] board...)? Commented Apr 18, 2014 at 10:40

1 Answer 1

2

You need to give size to last two dimensions of array in function definition, something like that for example

     #define y 10
     #define z 10

    void nextGeneration(state board[][y][z], int requestedGeneration, int boardHeight, int boardWidth) 
    {
       ....
    }


    int numberOfNeighbours(state board[][y][z], int requestedGeneration, int h, int w)
    {
       ....
    }

That is to give the system a hint how to calculate the index of an element when you trying to access it. Remember that array (no matter how many dimensions it has) is just a continuous piece of memory, so when you index into your array board[requestedGeneration][h][w] compiler generates code like that

*(board + requestedGeneration * 10 * 10 + h * 10 + w  )
Sign up to request clarification or add additional context in comments.

Comments

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.