strdup, _fstrdup
raw OCR
Copies a string into a newly created location.
Defined in header <string.h>
Syntax
#include <string.h>
Near version: char *strdupCconst char *s);
Far version: char far * far _fstrdup(const char far *s)
Portability
| DOS | UNIX | Windows | ANSI C | C++ only | |
|---|---|---|---|---|---|
| Near version | ■ | ■ | ■ | ||
| ■ | ■ |
Remarks
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.
See also
Example
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main (void)
{
char *dup_str, *string = "abode";
dup_str = strdup (string)
printf ("%s\n", dup_str);
free (dup_str);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Extraction notes
- heading `strdup, Jstrdup` read as `strdup, _fstrdup`
Stable link: /3.1/string.h/strdup/
· short form /3.1/strdup/