strdup(3C)
_________________________________________________________________
strdup function
Copy a string to an allocated area of memory.
_________________________________________________________________
Calling Sequence
char *strdup(), *newstr, *oldstr;
newstr = strdup(oldstr);
where old_str is a pointer to a character array.
new_str is a pointer to an allocated area containing
the new copy of the string.
Description
The strdup function first allocates enough memory to hold a
string with malloc, then moves a copy of the string into the
allocated area. The function then returns a pointer to the area
that contains the copy of the string. This function has the same
calling sequence as the strsave function.
The include file string.h defines this function.
Returns
The strsave function returns a pointer to the allocated area that
contains the string copy. It returns a null pointer if there is
no allocatable memory.
Related Functions
See also the strnsave and strsave functions.
Example
/* Program test for the strdup() function */
#include <stdio.h>
#include <string.h>
char *strdup(), *p;
void free();
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
strdup(3C)
main() {
p = strdup("Testing strdup.\n");
fputs(p, stdout);
free(p);
return 0;
}
A call to this program generates the output
Testing strdup.
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)