Malloc of arrays and structs within a struct

后端 未结 5 1120
无人共我
无人共我 2021-02-14 12:09

How does one malloc a struct which is inside another struct?

I would also like to malloc an array of items inside a struct and then realloc this array when needed, how i

5条回答
  •  旧巷少年郎
    2021-02-14 13:04

    typedef struct _A { int i; } A;
    typedef struct _B { int j;  A a} B;
    

    To get a single B:

    B *b = malloc(sizeof(B));
    

    To get an array of B:

    B *b = malloc(sizeof(B) * arrayLength);
    

提交回复
热议问题