Create a duplicate of a string
#include <string.h> char* strdup( const char* src );
libc
The strdup() function creates a duplicate of the string pointed to by src, and returns a pointer to the new copy.
The memory for the new string is obtained by using the malloc() function, and must be freed using the free() function.
A pointer to a copy of the string for success, or NULL.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( void )
{
char *dup;
dup = strdup( "Make a copy" );
printf( "%s\n", dup );
return EXIT_SUCCESS;
}
| Safety: | |
|---|---|
| Cancellation point | Yes |
| Interrupt handler | No |
| Signal handler | No |
| Thread | Yes |
free(), malloc(), strcpy(), strncpy()