strdup() - what does it do in C?

前端 未结 10 2524
情话喂你
情话喂你 2020-11-21 23:22

What is the purpose of the strdup() function in C?

10条回答
  •  情书的邮戳
    2020-11-22 00:28

    strdup and strndup are defined in POSIX compliant systems as:

    char *strdup(const char *str);
    char *strndup(const char *str, size_t len);
    

    The strdup() function allocates sufficient memory for a copy of the string str, does the copy, and returns a pointer to it.

    The pointer may subsequently be used as an argument to the function free.

    If insufficient memory is available, NULL is returned and errno is set to ENOMEM.

    The strndup() function copies at most len characters from the string str always null terminating the copied string.

提交回复
热议问题