0

i am getting the error "a3.c:221:20: error: storage size of ‘gold’ isn’t known" for all 4 of my item structs. 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.

4
  • 5
    You're using struct before defining it. Commented Mar 14, 2014 at 3:07
  • 3
    Put the struct item definition before your function! Commented Mar 14, 2014 at 3:08
  • Thanks, do either of you know how I would access the information i stored in the structs in a function? Commented Mar 14, 2014 at 3:14
  • @destroted this question is too strange and vague. You already do in lines like gold.Xposition = posX; Commented Mar 14, 2014 at 3:25

1 Answer 1

3

As keltar and nonsensickle already mentioned, you have to define struct item before you can use an instance of it:

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 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:

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.