簡體   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