分享
三行代码  ›  专栏  ›  技术社区  ›  Bobby.lock

通过函数传递malloc结构数组

c
  •  0
  • Bobby.lock  · 技术社区  · 1 周前

    我正在创建一个使用malloc创建一个生物结构和房间结构的游戏。在我的第一个函数中,我创建用户输入的房间数量。然后我向用户询问房间的状态,或者是12号,或者是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);
    }
    }
    
    1 回复  |  直到 1 周前
        1
  •  2
  •   Jake Armendariz    1 周前

    Malloc错误

    当前: 结构生物*所有生物=malloc(numberofbiomes*sizeof(numberofbiomes)); struct room*AllRooms=malloc(房间数*大小(房间数));

    更改为: 结构生物*所有生物=malloc(numberofbiomes*sizeof(结构生物)); 结构室*AllRooms=malloc(房间数*sizeof(结构室));