strcpy vs strdup

前端 未结 5 1930
抹茶落季
抹茶落季 2020-11-28 02:48

I read that strcpy is for copying a string, and strdup returns a pointer to a new string to duplicate the string.

Could you please explain

5条回答
  •  半阙折子戏
    2020-11-28 03:17

    strdup allocates memory for the new string on the heap, while using strcpy (or its safer strncpy varient) I can copy a string to a pre allocated memory on either the heap or the stack.

提交回复
热议问题