I have a problem when trying to initialize my nested structures.
On the compilation, there is no errors, but on the execution, I'm getting a segfault. In valgrind, I get
> Invalid read of size 8 at 0x402175: new_animal
> Address 0x38 is not stack'd, malloc'd or (recently) free'd
Here is the code :
void new_animal(int i, int j, int species){
struct animal * a;
if (array[i][j]==NULL)
{
a = malloc(sizeof(struct animal));
assert (a);
a->espece=espece;
if(array[i][j]->player==NULL)
{
a->player->id_fisherman=0;
strcpy(a->player->Name,"N"); //I want it set to NULL.
}
}
grille[i][j] = a;
}
And here is the two structures :
struct fisherman {
int id_fisherman;
char Name[10];
};
struct animal {
int species;
struct fisherman* player;
};
It worked fine until I added the fisherman. I don't know if it is due to the memory allocation, or when I'm initializing.
playerfield as astruct fishermanonly (without the*) or you do anothermalloc()to it, or initialize the pointer to somewhat valid (the address of something).