Using Designated Initializers with the Heap

前端 未结 2 1849
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 07:23

One can use designated initializers as shown below (for \"billy\") without issue, but when the same initialization approach is used on dynamic memory things wil

2条回答
  •  失恋的感觉
    2020-12-11 08:01

    Use a compound literal:

    *molly_ptr = ( struct student ){ .name = "molly", .age = 25 };
    

    This is almost identical to:

    struct student temp = { .name = "molly", .age = 25 };
    *molly_ptr = temp;
    

提交回复
热议问题