Allocation of memory for char array

前端 未结 10 851
孤独总比滥情好
孤独总比滥情好 2021-01-02 12:45

Let\'s say you have-

struct Person {
    char *name;
    int age;
    int height;
    int weight; 
 };

If you do-

struct Pe         


        
10条回答
  •  情话喂你
    2021-01-02 13:42

    It doesn't. You have to do that too.

    struct Person *who = malloc(sizeof(struct Person));
    who->name = malloc(sizeof(char) * 16); /* for a 15+1 character array */
    

提交回复
热议问题