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;
};
另外,我想知道我怎麼會去訪問我剛存入的結構不同功能的信息。
您在定義它之前使用struct。 – keltar
把'struct item'的定義放在你的函數之前! – nonsensickle
謝謝,你們中的任何一個知道我將如何訪問存儲在函數結構中的信息? – destroted