2014-04-18 3 views
1

내가 내가 타입 정의하고 열거 가능한을 사용했다, 그래서 셀이 DEAD 또는 ALIVE 여부를 표시하기 위해 타입 정의 state을 하였다 C. 삶의 게임을 만들고있어 형식 정의 불완전한 요소 유형이 있습니다. 나는 모든 세대의 모든 게시판을 보유하고있는 3D 배열을 만들었습니다. 배열을 함수의 매개 변수로 사용하려고하면 error: array type has incomplete element type이 표시됩니다. typedef를 잘못 했습니까? 사용자 정의 유형의 배열을 가질 수 없습니까?오류 : 배열 유형은

#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"); 
     } 
} 

실제 오류 메시지 : 어떤 도움이 많이 주시면 감사하겠습니다

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 

, 감사합니다 :) 다음은 내 코드입니다. 당신이 시도 할 때

+0

기다리과 같은 코드를 생성 어떤 것? 그렇지 않으면 함수가 마지막 이외의 "알 수없는"차원을 가진 다차원 배열 매개 변수를 허용 할 수 있다고 생각하지 않기 때문입니다. – Medinoc

+0

@Medinoc 그래서 내가 f (state [requestedGenerations] [boardHeight] [boardWidth] board ...)와 같은 것을해야한다고 말하는 것입니까? – shoham

답변

2

당신은 함수 정의에서 배열의 두 차원을 지속하기 위해 크기, 예를 들어 시스템을 어떻게 요소의 인덱스를 계산하는 힌트를 제공하는 것입니다

 #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][h][w] 컴파일러로 당신 지수는 C99 가변 길이 배열 또는를 사용하고, 그

*(board + requestedGeneration * 10 * 10 + h * 10 + w ) 
관련 문제