Create a duplicate of a string
#include <string.h> char* strdup( const char* src );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The strdup() function creates a duplicate of the string pointed to by src, and returns a pointer to the new copy.
The strdup() function allocates the memory for the new string by calling malloc(); it's up to you to release the memory by calling free(). |
A pointer to a copy of the string, or NULL if an error occurred.
#include <stdio.h> #include <string.h> #include <stdlib.h> int main( void ) { char *dup; dup = strdup( "Make a copy" ); printf( "%s\n", dup ); free (dup); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
free(), malloc(), memmove(), strcpy(), strncpy(), wcscpy(), wcsncpy(), wmemmove()