I have two structs. I am trying the make an array of 'struct bird' inside another struct 'struct nest'.
I am having a hard time allocating the correct amount of space for the bird array when I am creating the nest struct.
Below is my code.
struct bird {
int value;
};
typedef struct bird bird;
struct nest {
int nb_birds;
bird * * birds; //bird * = points to the bird struct, * birds = Array with size unknown
};
typedef struct nest nest;
nest * create_nest(int nb_birds) {
nest * n = (nest *) malloc(sizeof(nest));
n->nb_birds = nb_birds;
//This is where I am stuck
***n->birds = (bird *) malloc(sizeof(bird) * nb_birds);***
int i;
for(i = 0; i < nb_birds; i++)
n->birds[i]=NULL;
return n;
}
转载于:https://stackoverflow.com/questions/53248732/c-how-to-malloc-the-correct-amount-of-space-for-an-array-of-a-struct-inside-ano