c - expected identifier or '(' error when assigning variables to members of a struct array -


here header file im using:

 typedef struct room room;  struct room {  char name[21];        int num_doughnuts;    int num_milkshakes;  room* portal[4];   }; 

and code:

struct room* create_room() {    for(i = 0; < num_room; i++)    {       room[i] = (struct room*)malloc(sizeof(room));       printf("input room details\n");       fgets(input, max_length, stdin);       sscanf(input, "%s %i %i", room_name, &dnuts, &mshakes);        strcpy(room[i].name, room_name);  //assign name       room[i].num_doughnuts = dnuts;  //assign doughnuts       room[i].num_milkshakes = mshakes;   //assign milkshakes     }   return 0;  } 

and ive declared room struct in main function before create_room() being called

    struct room room[num_room]; 

im supposed user input store string , 2 integers user defined amount of rooms.

i keep getting error saying expected identifier or '(' these lines:

      room[i] = (struct room*)malloc(sizeof(room));       room[i].num_doughnuts = dnuts;  //assign doughnuts       room[i].num_milkshakes = mshakes;   //assign milkshakes 

i'm getting: unexpected type 'room': expected expression when trying assign name:

      strcpy(room[i].name, room_name);  //assign name 

there wrong way i've declared have no idea what. i've seen few of these problems , tried few different types of solutions keep getting similar error.

can shed light on please? i'm stuck! appreciated!

p.s if helps of these rooms have stored in doubly linked list after they've been created, portal[4] storing pointers other rooms. don't know if has bearing on question info never hurt anyone!

you write array room declared in main function. not visible in create_rooms. means room visible there type declared typedef, , that's reason why compiler complains.

note cannot declare array room outside main, you've used global identifier room type declared typedef. (why declaration within main not generate compile error? if done within main, local declaration hides global one, i.e. type room not visible within main.)

solution: declare array outside main , name differently.


Comments

Popular posts from this blog

Java sticky instances of class com.mysql.jdbc.Field aggregating -