_strdup Functions

Description

Duplicate strings.

#include <string.h> Required only for function declarations  

char *_strdup( const char *string );

char __far * __far _fstrdup( const char __far *string );

char __near * __far _nstrdup( const char __far *string );

string Source string  

Remarks

The _strdup function allocates storage space (with a call to malloc) for a copy of string and returns a pointer to the storage space containing the copied string. The function returns NULL if storage cannot be allocated.

The _fstrdup and _nstrdup functions provide complete control over the heap used for string duplication. The _strdup function returns a pointer to a copy of the string argument. The space for the string is allocated from the heap specified by the memory model in use. In large data models (that is, compact-, large-, and huge-model programs), _strdup allocates space from the far heap. In small data models (tiny-, small-, and medium-model programs), _strdup allocates space from the near heap.

The _strdup, _fstrdup, and _nstrdup functions operate on null-terminated strings. The string arguments to these functions are expected to contain a null character ('\0') marking the end of the string.

The _fstrdup function returns a far pointer to a copy of the string allocated in far memory (the far heap). As with the other model-independent functions, the syntax and semantics of these functions correspond to those of _strdup except for the sizes of the arguments and return values. The _nstrdup function returns a near pointer to a copy of the string allocated in the near heap (in the default data segment).

Return Value

The return values for these functions are described above.

Compatibility

_strdup

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_fstrdup, _nstrdup

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:None

See Also

strcat, strcmp, strncat, strncmp, strncpy, _strnicmp, strrchr, strspn

Example

/* STRDUP.C */

#include <string.h>

#include <stdio.h>

#include <conio.h>

#include <dos.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 );

}

Output

Original: This is the buffer text

Copy: This is the buffer text