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;
}
You want to allocate array of nb_birds
pointers to bird
structure, so size to allocation is nb_birds * sizeof(bird *)
.
Then you want to store pointer to this array, so cast should be to address of the first element — address of bird *
, i.e. bird **
.
Hence,
n->birds = (bird **) malloc(sizeof(bird *) * nb_birds);
p.s. If you want to allocate N
objects on which ptr
points to, you can write, or, at least, think about as
ptr = (typeof(ptr)) malloc(sizeof(*ptr) * N);
Update:
It should be noted that malloc
returns void *
pointer that compatible with any pointer type without explicit casting. So, quoted program line can be as short as
ptr = malloc(N * sizeof(*ptr));
Some programmers, despite them well informed about this void *
property, strongly prefer to use explicit cast in such cases. I'm not one of them, but I account such casts as stylistіc preference (like ()
for sizeof
operator). So I left the casting in code above because OP use it, and I thought it was his choice.
Neverthless it is needed (at least for answer completeness and for further readers) to note that such cast is unnecessary and excessive. .
Thank you Paul Ogilvie and chux for patient notes in the comments.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments