error: array type has incomplete element type typedef

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

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 :).

回答1:

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  ) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!