strdup, _mbsdup, _wcsdup

De RAD Studio
Aller à : navigation, rechercher

Remonter à string.h - Index

Header File

string.h, mbstring.h


char *strdup(const char *__s);
wchar_t *_wcsdup(const wchar_t *__s);
unsigned char *_mbsdup(const unsigned char *__s);


Copies a string into a newly created location.

strdup makes a duplicate of string s, obtaining space with a call to malloc. The allocated space is (strlen(s) + 1) bytes long. The user is responsible for freeing the space allocated by strdup when it is no longer needed.

Return Value

strdup returns a pointer to the storage location containing the duplicated string, or returns null if space could not be allocated.


POSIX ANSI C ANSI C++ Win32 Win64 macOS
strdup +
_mbsdup +
_wcsdup +


#include <stdio.h>
#include <string.h>
#include <alloc.h>

int main(void) {
  char *dup_str, *string = "abcde";
  dup_str = strdup(string);
  printf("%s\n", dup_str);
  return 0;