繁体   English   中英

通过函数传递malloc结构数组

[英]Passing malloc struct array through a function

我正在创建一个使用 malloc 创建生物结构和房间结构的游戏。 在我的第一个函数中,我创建了用户输入的房间数量。 然后我向用户询问房间的状态 1 2 或 3,然后询问东南西北和西部的电线。 这就是这个功能的全部内容。 到目前为止一切都很好。 然后当我创建我的生物时,我通过用户输入来初始化它们。 我询问用户只能是 0 1 或 2 的生物类型,然后询问生物的位置,该位置将与房间号相关联。 因此,如果生物位置为 1,则它在房间 1 中。但由于某种原因,它在生物功能中更改了我房间中的绳索。 从字面上看,不知从何处改变了它们。

例如,我输入 4 个房间,第一个房间 0,1,2,3,4 然后第二个房间 3,1,2,4,3 然后相同的房间 3 和 4。 目前,绳索并不重要,但我的问题是通过生物功能,它会因某种原因改变我的绳索。 有人可以帮帮我吗。 我知道这是很多代码,但我没有想法

struct room
{
   int roomNum;
   int creaturesTotal;
   int roomStatus;
   int roomTotal;
   int north;
   int south;
   int east;
   int west;
};

struct Creatures
{
  int creatureType;
  int creatureNum;
  int location;
};

 int main()
{
int numberofrooms = 0;
int numberofcreatures = 0;


/*ask user for rooms and creatures*/
printf("How many rooms? Max 10 rooms: ");
scanf("%d",&numberofrooms);
/*make sure its under 10 rooms*/
while(numberofrooms > 10)
{
    printf("\nToo many rooms!\n");
    printf("How many rooms? Max 10 rooms: ");
    scanf("%d",&numberofrooms);
}

printf("How many creatures? Max 100 creatures: ");
scanf("%d",&numberofcreatures);

while(numberofcreatures > 100)
{
    printf("\nToo many creatures! MAX 100 creatures please!\n");
    printf("How many creatures? Max 100 creatures: ");
    scanf("%d",&numberofcreatures);
}

  struct Creatures*AllCreatures = malloc(numberofcreatures * sizeof(numberofcreatures));
  struct room *AllRooms = malloc(numberofrooms * sizeof(numberofrooms));

  createRooms(numberofrooms, AllRooms);
  createCreatures(numberofcreatures,AllCreatures,AllRooms);
 }

void createCreatures(int numberofcreatures, struct Creatures* AllCreatures,struct room* AllRooms)
{
  int location;

  int counter = 0;
  int PC = 0;

//ask the user for creatures and check the inputs
for(int i=0; i < numberofcreatures; i++)
{
    int creatureType;

        printf("\nType of Creature, Location: ");
        scanf("%d%d",&creatureType,&location);

        //if room is full
        while(AllRooms[location].roomTotal == 10)
        {
            printf("\nRoom is already full!\n");
            printf("\nType of Creature, Location: ");
            scanf("%d%d",&creatureType,&location);

            //make sure isnt invalid creature num in nested while loop
            while(creatureType < 0 || creatureType > 2)
            {
                printf("\ninvalid creature type\n");
                printf("\nType of Creature, Location: ");
                scanf("%d%d",&creatureType,&location);
            }
        }

        //if room isnt full but invalid creature type
        while(creatureType < 0 || creatureType > 2)
        {
            printf("\ninvalid creature type\n");
            printf("\nType of Creature, Location: ");
            scanf("%d%d",&creatureType,&location);
        }

         if(creatureType == 0)
         {
             PC++;
             while(PC > 1)
            {
                 printf("\nThere is already a PC player, enter again");
                 printf("\nType of Creature, Location: ");
                 scanf("%d%d",&creatureType,&location);
                 if(creatureType == 1 || creatureType == 2)
                 {
                     PC--;
                 }
            }
         }


    //print out the creatures with the room numbers
    AllCreatures[i].location = location;
    AllCreatures[i].creatureType = creatureType;
    AllCreatures[i].creatureNum = counter;

    //AllRooms[AllCreatures[i].location].roomTotal = AllRooms[AllCreatures[i].location].roomTotal + 1;
    counter++;
}

for(int i=0; i < numberofcreatures; i++)
{
    printf("\n Creature num %d, type %d, location %d\n",AllCreatures[i].creatureNum, AllCreatures[i].creatureType,AllCreatures[i].location);
}
}

//create all rooms
void createRooms(int numberofrooms,struct room* AllRooms)
{
   int counter = 0;
   int status;
   int north;
   int south;
   int east;
   int west;

//ask the user for the cords
for(int i =0; i < numberofrooms;i++)
{
    printf("Room Number %d state north south east west: ",counter);
    scanf("%d%d%d%d%d",&status,&north,&south,&east,&west);
    AllRooms[i].roomStatus = status;
    AllRooms[i].north = north;
    AllRooms[i].south = south;
    AllRooms[i].east = east;
    AllRooms[i].west = west;
    AllRooms[i].roomNum = counter;
    AllRooms[i].roomTotal = 0;
    counter++;
}

//print out the cords
for(int i =0; i < numberofrooms;i++)
{
    printf("\n%d,%d,%d,%d,%d\n",AllRooms[i].roomStatus,AllRooms[i].north,AllRooms[i].south,AllRooms[i].east,AllRooms[i].west);
}
}

Malloc 错误不确定这是否是问题的原因,但您没有分配足够的空间。

当前: struct Creatures*AllCreatures = malloc(numberofcreatures * sizeof(numberofcreatures)); struct room *AllRooms = malloc(numberofrooms * sizeof(numberofrooms));

更改为: struct Creatures*AllCreatures = malloc(numberofcreatures * sizeof(struct Creatures)); struct room *AllRooms = malloc(numberofrooms * sizeof(struct room));

这可能是问题所在,但如果它不能解决它,请对此发表评论,我会更红以查看是否可以解决它。

暂无
暂无

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

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