Using Designated Initializers with the Heap

前端 未结 2 1843
被撕碎了的回忆
被撕碎了的回忆 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;
    
    0 讨论(0)
  • 2020-12-11 08:20

    You have to write:

    *molly_ptr = (struct student){
                                    .name = "molly",
                                    .age = 25
                                 };
    
    0 讨论(0)
提交回复
热议问题