Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      strdup

      From cppreference.com
      <c‎ |string‎ |byte
       
       
       
       
      Defined in header<string.h>
      char*strdup(constchar*src);
      (since C23)

      Returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to bysrc. The space for the new string is obtained as if themalloc was invoked. The returned pointer must be passed tofree to avoid a memory leak.

      If an error occurs, a null pointer is returned anderrno might be set.

      Contents

      [edit]Parameters

      src - pointer to the null-terminated byte string to duplicate

      [edit]Return value

      A pointer to the newly allocated string, or a null pointer if an error occurred.

      [edit]Notes

      The function is identical to thePOSIX strdup.

      [edit]Example

      Run this code
      #include <string.h>#include <stdio.h>#include <stdlib.h> int main(void){constchar*s1="Duplicate me!";char*s2= strdup(s1);printf("s2 =\"%s\"\n", s2);free(s2);}

      Output:

      s2 = "Duplicate me!"

      [edit]See also

      (C23)
      allocates a copy of a string of specified size
      (function)[edit]
      copies one string to another
      (function)[edit]
      allocates memory
      (function)[edit]
      deallocates previously allocated memory
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/byte/strdup&oldid=136177"

      [8]ページ先頭

      ©2009-2025 Movatter.jp