RAD Studio
ContentsIndex
PreviousUpNext
strdup, _mbsdup, _wcsdup

Header File 

string.h, mbstring.h 

Category 

Memory and String Manipulation Routines 

Prototype 

char *strdup(const char *s); 

wchar_t *_wcsdup(const wchar_t *s); 

unsigned char *_mbsdup(const wchar_t *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. 

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;
}

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
strdup 
 
 
 
_mbsdup 
 
 
 
_wcsdup 
 
 
 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!