Why do we need strdup()?

前端 未结 5 1880
花落未央
花落未央 2021-02-20 07:24

While I was working on an assignment, I came to know that we should not use assignments such as :

 char *s=\"HELLO WORLD\";

Programs using such

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-20 08:27

    String literals are stored in the program's data segment. Manipulating their pointers will modify the string literal, which can lead to... strange results at best. Use strdup() to copy them to heap- or stack-allocated space instead.

提交回复
热议问题