简体   繁体   中英

Losing values in struct array after leaving for loop in C

I am about 5 days into C programming and I am having a bit of trouble understanding what exactly is happening in my code. I populate an array of room structs on the heap the rooms each have integer values I fill with user input right after I allocate space. There is an array of creature structs inside each room struct. I fill the fields inside each room with int values from stdin, however after I fill them and leave the for loop the values seem to reset and I get random values in their place similarly to when I allocate the memory on the heap beforehand. Why I am so confused is that when I fill my creature_array with the values from stdin I do it almost in the same process and everything looks fine and those values can be accessed where needed in my game. Any help is greatly appreciated thanks! My code for filling rooms and creatures is below.

typedef struct {
   int type;
   int room_number;
   int creat_number;
} creature;

typedef struct {
   struct room *north; //refernce to neighbor
   struct room *south;
   struct room *east;
   struct room *west;
   int n,s,e,w;
   int room_name, state;
   creature creature_array[10];
} room; 

void addCreature(int rm, int t, int cm) {
   int i = 0;
   int f = 0;
  for (; i < 10; i++) {
      if (ptr[rm].creature_array[i].type != 0 && ptr[rm].creature_array[i].type != 1 && ptr[rm].creature_array[i].type !=2) {
         ptr[rm].creature_array[i].creat_number = cm;
         ptr[rm].creature_array[i].type = t;
         ptr[rm].creature_array[i].room_number = rm;
         break;
      } else {
         f++;
         if (f == 9) {
            printf("Room ");
            printf("%d", ptr[rm].room_name);
            printf(" is full.\n");
         }
       }
     }
   }

int main(void) {
   setbuf(stdout, NULL);
   int state, north, south, east, west;
   printf("Welcome!\nHow many rooms?\n");
   int num_r;
   scanf("%d", &num_r);
   ptr = (room *)malloc(num_r * (sizeof (room)));
   int i = 0;
for (; i < num_r; i++) {
      scanf("%d %d %d %d %d", &state, &north, &south, &east, &west);
      ptr[i].room_name=i;
      ptr[i].state = state;
      ptr[i].n=north;
      ptr[i].s=south;
      ptr[i].e=east;
      ptr[i].w=west;
   }
   printf("How many creatures?\n");
   int room_num, type, creat_num;
   int num_of_c;
   scanf("%d", &num_of_c);
   int p = 0;
   int PC_count = 0;
   int creat_count = 0;
 for (; p < num_of_c; p++) {
      creat_num = creat_count++;
      scanf("%d %d", &type, &room_num);
      if (type == 0) {
         PC_count++;
         if (PC_count > 1) {
            printf("Too many PC players\n");
            exit(0);
         }
         addCreature(room_num,type,creat_num);
         pc = &ptr[room_num].creature_array[p];
      } else {
         addCreature(room_num,type,creat_num);
      }
   }
}

The line

if (ptr[rm].creature_array[i].type != 0||1||2)

is equivalent to:

if (ptr[rm].creature_array[i].type != (0||1||2) )

is equivalent to:

if (ptr[rm].creature_array[i].type != 1 )

Is that what you wanted?

I suspect you wanted:

if ( (ptr[rm].creature_array[i].type != 0) &&
     (ptr[rm].creature_array[i].type != 1) &&
     (ptr[rm].creature_array[i].type != 2) )

addCreature()使用ptr[room_num].creature_array未初始化值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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