2014-03-14 77 views
0

我收到錯誤「a3.c:221:20:錯誤:存儲大小'黃金'未知」爲我的所有4個項目結構。我的代碼如下:使用結構錯誤,C

void parser(int argc, char **argv) 
{ 
FILE * rooms; 
char * theString; 
char * theToken; 
int numberElements; 
int side; 
int k; 

int placeInt; 
int posX; 
int posY; 
char a[ROOM_STRING_LENGTH]; 
numberElements = 0; 
rooms = fopen(argv[1], "r"); 

if(rooms == NULL) 
{ 
    printf("error opening file\n"); 
} 
while(fgets(a, ROOM_STRING_LENGTH, rooms) != NULL) 
{ 

    theString = malloc((sizeof(char)*(strlen(a)+1))); 
    strcpy(theString, a); 

    for(theToken = strtok(theString, " "); theToken; theToken = strtok(NULL, " ")) 
    { 
     printf("the next token: %s\n", theToken); 
     if(theToken[0] == '1') 
     { 

     } 
     if(theToken[0] == 'd') 
     { 
      switch(theToken[1]) 
      { 
       case 'e': 
       { 
        side = 1; 
        placeInt = theToken[2] - '0'; 
        printf("the side: %d, the place: %d\n", side, placeInt); 
        break; 
       } 
       case 'w': 
       { 
        side = 2; 
        placeInt = theToken[2] - '0'; 
        printf("the side: %d, the place: %d\n", side, placeInt); 
        break; 
       } 
       case 's': 
       { 
        side = 3; 
        placeInt = theToken[2] - '0'; 
        printf("the side: %d, the place: %d\n", side, placeInt); 
        break; 
       } 
       case 'n': 
       { 
        side = 4; 
        placeInt = theToken[2] - '0'; 
        printf("the side: %d, the place: %d\n", side, placeInt); 
        break; 
       } 
       default: 
       { 
        break; 
       } 
      }  
     } 

     else if(theToken[0] == 'g' || theToken[0] == 'm' || theToken[0] == 'p' || theToken[0] == 'h') 
     { 
      k = 0; 
      while(k <= (strlen(theToken))) 
      { 

       switch(theToken[k]) 
       { 
        case 'g': 
        posY = theToken[1] - '0'; 
        posX = theToken[3] - '0'; 
        struct item gold; 
        gold.Xposition = posX; 
        gold.Yposition = posY; 
        printf("the y position: %d, the x position: %d\n", posY, posX); 
        break; 

        case 'm': 
        posY = theToken[1] - '0'; 
        posX = theToken[3] - '0'; 
        struct item monster; 
        monster.Xposition = posX; 
        monster.Yposition = posY; 
        printf("the y position: %d, the x position: %d\n", posY, posX); 
        break; 

        case 'p': 
        posY = theToken[1] - '0'; 
        posX = theToken[3] - '0'; 
        struct item potion; 
        potion.Xposition = posX; 
        potion.Yposition = posY; 
        printf("the y position: %d, the x position: %d\n", posY, posX); 
        break; 

        case 'h': 
        posY = theToken[1] - '0'; 
        posX = theToken[3] - '0'; 
        struct item hero; 
        hero.Xposition = posX; 
        hero.Yposition = posY; 
        printf("the y position: %d, the x position: %d\n", posY, posX);       
        break; 
       } 
       k++; 
      } 
     } 
     else if(theToken == NULL) 
     { 
      printf("end of file"); 
     } 
     numberElements++; 
    } 

    if(theToken == NULL) 
    { 
     printf("You've reached the end of the line\n"); 
    } 
    printf("%d\n", numberElements); 
} 

free(theString); 
fclose(rooms); 
} 



struct item 
{ 
    int Xposition; 
    int Yposition; 
}; 

另外,我想知道我怎麼會去訪問我剛存入的結構不同功能的信息。

+5

您在定義它之前使用struct。 – keltar

+3

把'struct item'的定義放在你的函數之前! – nonsensickle

+0

謝謝,你們中的任何一個知道我將如何訪問存儲在函數結構中的信息? – destroted

回答

3

由於keltar和nonsensickle已經提到的,你必須定義struct item纔可以使用它的一個實例:

struct item { int x; int y; }; // this must come first 

// ... 

struct item item1 {4, 2}; 

你可以,但是,使用指針的定義之前,只要你已經聲明的結構:

struct item; // declaration, no definition 

// ... 

struct item *pitem1; 

// ... 

struct item { int x; int y; }; // defined later 

到另一個函數中使用struct的成員,你可以通過無論是structstruct*到功能:

void use_struct (struct item i) 
{ 
    int a = i.x, b = i.y; 
} 

void use_struct_pointer (struct item *pi) 
{ 
    int a = pi->x, b = pi->y; 
} 

int main() 
{ 
    struct item i {4, 2}; 
    use_struct(i); 
    use_struct_pointer(&i); 
    return 0; 
}