The _strdup function calls malloc to allocate storage
space for a copy of strSource and then copies strSource to the allocated space.
_wcsdup and _mbsdup are wide-character and
multibyte-character versions of _strdup. The arguments and return value of
_wcsdup are wide-character strings; those of _mbsdup are multibyte-character
strings. These three functions behave identically otherwise.
_UNICODE & _MBCS not defined:Use _strdup
defined in <string.h>
_MBCS defined:Use _mbsdup defined in
<mbstring.h>
_UNICODE defined: Use _wcsdup defined in
<string.h> or <wchar.h>
TCHAR.H routine : Use _tcsdup
Syntax:
char *_strdup(const char *strSource);
char *_tcsdup(const char *strSource);
wchar_t *_wcsdup(const wchar_t *strSource);
unsigned char *_mbsdup(const unsigned char
*strSource);
strSource:
Null-terminated source string.
Return Value:
Each of these functions returns a pointer to the
storage location for the copied string or NULL if storage cannot be allocated.
Example:
// _strdup.c
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
void main( void )
{
char buffer[] = "This is the buffer
text";
char *newstring;
printf( "Original: %s\n",
buffer );
newstring = _strdup( buffer );
printf("Copy:
%s\n", newstring );
free( newstring );
getch();
}
Output:
Original: This is the buffer text
Copy:This is the buffer text
// _tcsdup.cpp
#include <string.h>
#include <iostream.h>
#include <malloc.h>
#include <conio.h>
int main( void )
{
char buffer[] = "This is the buffer
text";
char *newstring;
cout<< "Original:"<<
buffer;
newstring = _strdup( buffer );
cout<<"Copied
string:"<<newstring;
free(newstring);
getch();
return 0;
return 0;
}
Output:
Original: This is the buffer text
Copied string:This is the buffer text
No comments:
Post a Comment