This probably doesn't make any sense
Indeed, it is pointless and doesn't make any sense. But if you for reasons unknown persist on doing it, then the best way is likely to use compound literals, rather than dynamic memory allocation.
struct b b =
{
&(struct a){10},
20
};
This way, the member struct has the same scope as the variable b
, because the scope of a compound literal is the same as for a local variable. This way, no additional malloc/free is needed.
Please note that compound literals are a superfluous feature of the C language. The above C code will yield exactly the same machine code as:
struct a a = {10};
struct b b =
{
&a,
20
};
The difference is that the latter is more readable.