Hello fellow C programmers,
this is my first post on this forum so first and foremost, hello everybody.
Now for my question. The project I am working on is hunter-prey simulation game.
My error lays in the following part.
The function below will get parameters from another function. These parameters will tell the function how many meerkats (prey) and hunters will have to be created. It will also tell the function how big the world will be (nSquare, if nSquare is 4, the world will be created as a 4 x 4 world). The function will then allocate these 'animals' (structs). It will also calculate how many ground "animals" (non hunter nor prey animal structs) have to be created by doing nSquare^2 - nHunters - nMeerkats. After this all structs will be allocated. The square will be allocated as a two dimensional array. After that I want to put animals on the world by starting with meerkats. As you can see in the struct square, there can be 5 animals per square. After I'm finished with the meerkats I will put grounds "animals" (notice again that these aren't real animals, I say animals because of the struct name, it's just plain ground) on the world until there are no more. After that the remaining squares will have to be filled with hunters. There is where it goes wrong. When I give the world pointer as a parameter to my print function and try to print the rank of all squares of the world, I get a segmentation fault.
I'm guessing (I know) I'm doing something wrong at the part where I try to put everything in my 2D array, but I'm stuck at it. Can anyone help me?
Just ask me if you need more explanation on a part.
Kind regards,
boxden
Struct declaration:
Code:
struct Square
{
struct Animal* animal[5];
struct Room* entrance;
};
struct Animal
{
char rank; /* M meerkat H hunter G ground */
boolean danger; /* default false. when guard detects danger, his
boolean will turn true, toggling the danger boolean
to true for all animals (meerkats) */
boolean isGuard;
};
note: typedef int boolean;
Create surface function and print function:
Code:
struct Square *createSurface(int nSquare, int nMeerkat, int nHunter, struct Room *entrance)
{
int i, j, k, meerCounter = 0, huntCounter = 0, groundCounter = 0, nGround;
struct Square **square ;
struct Animal *meerkat, *hunter, *ground;
nGround = pow(nSquare, 2) - nMeerkat - nHunter;
/* CREATION OF THE STRUCTS THE USER GAVE AS INPUT */
meerkat = (struct Animal*)malloc(nMeerkat * sizeof(struct Animal));
hunter = (struct Animal*)malloc(nHunter * sizeof(struct Animal));
ground = (struct Animal*)malloc(nGround * sizeof(struct Animal));
/* INITIALISATION OF THE STRUCTS CREATED ABOVE */
for (i = 0; i < nMeerkat; i++)
{
meerkat[i].rank = 'M';
meerkat[i].danger = FALSE;
meerkat[i].isGuard = FALSE;
}
meerkat[0].isGuard = TRUE; /* first animal created is a guard */
for (i = 0; i < nHunter; i++)
{
hunter[i].rank = 'H';
hunter[i].danger = FALSE; /* not important */
hunter[i].isGuard = FALSE; /* not important */
}
for (i = 0; i < nGround; i++)
{
ground[i].rank = 'G';
ground[i].danger = FALSE;
ground[i].isGuard = FALSE;
}
/* CREATION OF THE WORLD */
square = (struct Square**)malloc(nSquare * sizeof(struct Square *)); /* two dimensional allocation */
for (i = 0; i < nSquare; i++)
square[i] = (struct Square*)malloc(nSquare * sizeof(struct Square));
/* PUTTING ANINALS (MEERKAT, GROUND, HUNTER) ON WORLD */
for (i = 0; i < nSquare; i++) /* rows */
{
for (j = 0; j < nSquare; j++) /* columns */
{
for (k = 0; k < 5 && meerCounter < nMeerkat; k++, meerCounter++) /* 5 animals per square */
square[i][j].animal[k] = &meerkat[meerCounter];
}
}
for (i; i < nSquare; i++) /* rows */ /* I want i to continue where it stopped at the previous for loop */
{
for (j; j < nSquare; j++) /* columns */
{
for (k = 0; k < 5 && groundCounter < nGround; k++, groundCounter++) /* 5 animals per square */
square[i][j].animal[k] = &ground[groundCounter];
}
}
for (i = nSquare - 1; i > 0; i--)
{
for (j = nSquare - 1; j > 0; j--)
{
for (k = 0; k < 5 && huntCounter < nHunter; k++, huntCounter++)
square[i][j].animal[k] = &hunter[huntCounter];
}
}
/* first animal created was a guard, first animal is on first square so this square will be an entrance */
/* has to point to an exit */
square[0][0].entrance = entrance;
print(square, nSquare);
return (&square[0][0]);
}
void allocIni()
{
/* allocate and ini stuff in here and give pointers back to surface function */
}
void print(struct Square **world, int nSquare)
{
int i, j, k;
for (i = 0; i < nSquare; i++)
{
for (j = 0; j < nSquare; j++)
{
for (k = 0; k < 5; k++)
{
printf("RANK: %c X: %i Y: %i Nde animal: %i \t", world[i][j].animal[k]->rank, i, j, k);
}
printf("\n");
}
}
}