Passing malloc struct array through a function

Bobby.lock

I'm creating a game that creates a struct of creatures and struct of rooms using malloc. In my first function, i create the amount of rooms that the user inputs. I then ask the user for the status of the room either 1 2 or 3 then ask the cords for north south east and west. That is all for this function. Everything is fine up to this point. Then when i create my creatures, I initialize them through input via user. I ask the user for the creature type which can only be 0 1 or 2, then ask for the location of the creature and the location will be associated with the room number. So if creature location is 1 then its in room 1. but for some reason it changes my cords in my rooms in the creature function. Literally changes them out of no where.

Example, I enter for 4 rooms, first room 0,1,2,3,4 then second room 3,1,2,4,3 then same for room 3 and four. For now, the cords dont matter but my problem is that through the creature function, it changes my cords for some reason. Can someone please help me. I know this is a lot of code but I'm out of ideas

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);
}
}
Jake Armendariz

Malloc Error Not sure if this is the cause of the problem, but you aren't allocating enough space.

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

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

This could be the problem, but if it doesn't fix it comment on this and I will red more to see if I can solve it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing an array of 'typedef struct' to a function

From Dev

Passing entire struct array to function

From Dev

Passing array of struct to function in C

From Dev

passing struct array as parameter to a function

From Dev

Getting a warning when passing struct through function

From Dev

Passing strings through and array to a struct in C

From Dev

Passing a Struct Array through a TableViewCell to another ViewController

From Dev

Passing through array that is in an object into a function

From Dev

Passing an Array through Function Then Looping

From Dev

Passing string to init function and storing it in a malloc'd struct

From Dev

Passing char array to a struct array within a function

From Dev

Passing struct pointer through a function then accessing struct data

From

Passing dynamic array struct to a function Golang

From Dev

c++ passing struct array to function

From Dev

Passing struct array to function and calculating average in C?

From Dev

Passing an array from a struct as an argument to a function

From Dev

Malloc 'ing array of struct in struct

From Dev

Malloc an array inside a struct

From Dev

c malloc array of struct

From Dev

Python: passing memmap array through function?

From Dev

Passing numpy array through scipy fitting function

From Dev

Passing an array to function a looping through it in C

From Dev

Filter custom struct array through function in Swift

From Dev

struct tm becomes PST when passing through a function

From Dev

Passing an element of an array (struct) through pthread_create

From Dev

C - dynamic array of typedef struct with in-function malloc

From Dev

passing a struct argument in a struct function

From Dev

Passing struct pointer to two functions and then calling malloc

From Dev

struct function passing and returning

Related Related

  1. 1

    Passing an array of 'typedef struct' to a function

  2. 2

    Passing entire struct array to function

  3. 3

    Passing array of struct to function in C

  4. 4

    passing struct array as parameter to a function

  5. 5

    Getting a warning when passing struct through function

  6. 6

    Passing strings through and array to a struct in C

  7. 7

    Passing a Struct Array through a TableViewCell to another ViewController

  8. 8

    Passing through array that is in an object into a function

  9. 9

    Passing an Array through Function Then Looping

  10. 10

    Passing string to init function and storing it in a malloc'd struct

  11. 11

    Passing char array to a struct array within a function

  12. 12

    Passing struct pointer through a function then accessing struct data

  13. 13

    Passing dynamic array struct to a function Golang

  14. 14

    c++ passing struct array to function

  15. 15

    Passing struct array to function and calculating average in C?

  16. 16

    Passing an array from a struct as an argument to a function

  17. 17

    Malloc 'ing array of struct in struct

  18. 18

    Malloc an array inside a struct

  19. 19

    c malloc array of struct

  20. 20

    Python: passing memmap array through function?

  21. 21

    Passing numpy array through scipy fitting function

  22. 22

    Passing an array to function a looping through it in C

  23. 23

    Filter custom struct array through function in Swift

  24. 24

    struct tm becomes PST when passing through a function

  25. 25

    Passing an element of an array (struct) through pthread_create

  26. 26

    C - dynamic array of typedef struct with in-function malloc

  27. 27

    passing a struct argument in a struct function

  28. 28

    Passing struct pointer to two functions and then calling malloc

  29. 29

    struct function passing and returning

HotTag

Archive