strdup, _mbsdup, _wcsdup

From RAD Studio
Jump to: navigation, search

Go Up to string.h Index


Header File

string.h, mbstring.h

Prototype

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

Description

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.

Portability

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

Example

#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);
  free(dup_str);
  return 0;
}