简体   繁体   English

使用结构,C错误

[英]error using structs, C

i am getting the error "a3.c:221:20: error: storage size of 'gold' isn't known" for all 4 of my item structs. 我收到针对所有4个项目结构的错误“ a3.c:221:20:错误:'gold'的存储大小未知”。 My code is as follows: 我的代码如下:

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;
};

Also, I was wondering how i would go about accessing the information i just stored into those structs in a different function. 另外,我想知道我将如何去访问刚刚存储在具有不同功能的那些结构中的信息。

As keltar and nonsensickle already mentioned, you have to define struct item before you can use an instance of it: 正如keltar和nonsensickle所提到的,您必须先定义struct item然后才能使用它的实例:

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

// ...

struct item item1 {4, 2};

You could, however, use a pointer before the definition, as long as you have already declared the struct: 但是,只要已经声明了struct,就可以在定义之前使用指针:

struct item; // declaration, no definition

// ...

struct item *pitem1;

// ...

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

To use a struct 's members in another function, you could pass either a struct or a struct* to that function: 要在另一个函数中使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM