Allocation of memory for char array

前端 未结 10 817
孤独总比滥情好
孤独总比滥情好 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:24

    It will allocate 4 bytes for the name pointer, but no space for the "real" string. If you try to write there you will seg fault; you need to malloc (and free) it separately.

    Using string (in C++) can save you some headache

提交回复
热议问题