C 错误:数组类型的元素类型typedef不完整
我正在用C语言做一个生命游戏。我被告知尽可能使用typedef和enum,所以我制作了一个typedefC 错误:数组类型的元素类型typedef不完整,c,C,我正在用C语言做一个生命游戏。我被告知尽可能使用typedef和enum,所以我制作了一个typedef状态,以指示单元格是死的还是活的。 我制作了一个3d阵列,可以容纳所有世代的所有电路板。 当我尝试将数组用作函数的参数时,它会显示错误:数组类型的元素类型不完整。我做错事了吗?不能有用户定义类型的数组吗? 这是我的密码: #include <stdio.h> #include <stdlib.h> typedef enum { DEAD, ALIVE } state;
状态
,以指示单元格是死的还是活的。
我制作了一个3d阵列,可以容纳所有世代的所有电路板。
当我尝试将数组用作函数的参数时,它会显示错误:数组类型的元素类型不完整
。我做错事了吗?不能有用户定义类型的数组吗?
这是我的密码:
#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");
}
}
非常感谢您的帮助,谢谢:)。您需要在函数定义中给出数组最后两个维度的大小,例如
#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)
{
....
}
这是给系统一个提示,当您试图访问某个元素时,如何计算该元素的索引。请记住,数组(不管它有多少个维度)只是一个连续的内存块,所以当您索引到数组时,编译器会生成这样的代码
*(board + requestedGeneration * 10 * 10 + h * 10 + w )
等等,你是在用C99可变长度数组还是什么?因为否则,我认为函数不能接受除最后一个维度以外的任何“未知”维度的多维数组参数。@Medinoc那么你是说我应该执行类似于f(state[requestedGenerations][boardHeight][boardWidth]board?
*(board + requestedGeneration * 10 * 10 + h * 10 + w )