When to use malloc for char pointers

前端 未结 5 886
醉话见心
醉话见心 2020-12-23 00:19

I\'m specifically focused on when to use malloc on char pointers

char *ptr;
ptr = \"something\";
...code...
...code...
ptr = \"something else\";
5条回答
  •  礼貌的吻别
    2020-12-23 00:30

    Everytime the size of the string is undetermined at compile time you have to allocate memory with malloc (or some equiviallent method). In your case you know the size of your strings at compile time (sizeof("something") and sizeof("something else")).

提交回复
热议问题