strdup() - what does it do in C?

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

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

10条回答
  •  余生分开走
    2020-11-22 00:28

    char * strdup(const char * s)
    {
      size_t len = 1+strlen(s);
      char *p = malloc(len);
    
      return p ? memcpy(p, s, len) : NULL;
    }
    

    Maybe the code is a bit faster than with strcpy() as the \0 char doesn't need to be searched again (It already was with strlen()).

提交回复
热议问题