C, How to malloc the correct amount of space for an array of a struct inside another struct?

seamus

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;
}
ReAl

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Malloc an array inside a struct

From Dev

How do I use malloc for an array of structs inside another struct that has been created using malloc

From Dev

c malloc array of struct

From Dev

Assign a struct to another struct inside a struct in C

From Dev

How to PInvoke in C# an array of structs inside another struct

From

How to access golang Struct Array inside another Struct?

From Dev

Creating an array of struct inside another struct and accessing the members of the struct inside the array in C

From Dev

c++ malloc for array in struct

From Dev

Creating array of struct inside another struct

From Dev

Dynamic array inside struct and malloc fail

From Dev

how do I assign a value to a struct array inside a struct in c?

From Dev

How to delete an array that is in a struct inside a struct c++

From Dev

How to correctly malloc a struct in C

From Dev

c how to initial struct array member from another struct variable

From Dev

How to get struct array (or pointer) that is part of another struct? in C

From Dev

Create variable amount of struct objects inside a struct (C)

From Dev

Malloc 'ing array of struct in struct

From Dev

C declare an array for a struct inside a struct

From Dev

struct with a pointer inside that is an array (C)

From Dev

Initialise array inside struct in C

From Dev

Swift filter an array inside a struct by another array

From Dev

how to do malloc to array from struct

From Dev

how to write in a file a struct that has an array with Malloc

From Dev

c - Right justifying an int inside of a struct inside of another struct

From Dev

how to copy to array inside struct?

From Dev

How can I update a table that has struct inside a struct and both structs are inside an array? ARRAY<STRUCT<STRUCT>>

From

malloc for struct and pointer in C

From Dev

Trouble with struct malloc in C

From Dev

Using malloc with struct in C

Related Related

  1. 1

    Malloc an array inside a struct

  2. 2

    How do I use malloc for an array of structs inside another struct that has been created using malloc

  3. 3

    c malloc array of struct

  4. 4

    Assign a struct to another struct inside a struct in C

  5. 5

    How to PInvoke in C# an array of structs inside another struct

  6. 6

    How to access golang Struct Array inside another Struct?

  7. 7

    Creating an array of struct inside another struct and accessing the members of the struct inside the array in C

  8. 8

    c++ malloc for array in struct

  9. 9

    Creating array of struct inside another struct

  10. 10

    Dynamic array inside struct and malloc fail

  11. 11

    how do I assign a value to a struct array inside a struct in c?

  12. 12

    How to delete an array that is in a struct inside a struct c++

  13. 13

    How to correctly malloc a struct in C

  14. 14

    c how to initial struct array member from another struct variable

  15. 15

    How to get struct array (or pointer) that is part of another struct? in C

  16. 16

    Create variable amount of struct objects inside a struct (C)

  17. 17

    Malloc 'ing array of struct in struct

  18. 18

    C declare an array for a struct inside a struct

  19. 19

    struct with a pointer inside that is an array (C)

  20. 20

    Initialise array inside struct in C

  21. 21

    Swift filter an array inside a struct by another array

  22. 22

    how to do malloc to array from struct

  23. 23

    how to write in a file a struct that has an array with Malloc

  24. 24

    c - Right justifying an int inside of a struct inside of another struct

  25. 25

    how to copy to array inside struct?

  26. 26

    How can I update a table that has struct inside a struct and both structs are inside an array? ARRAY<STRUCT<STRUCT>>

  27. 27

    malloc for struct and pointer in C

  28. 28

    Trouble with struct malloc in C

  29. 29

    Using malloc with struct in C

HotTag

Archive