[Previous] [Contents] [Next]

strdup()

Create a duplicate of a string

Synopsis:

#include <string.h>

char* strdup( const char* src );

Library:

libc

Description:

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.

Returns:

A pointer to a copy of the string for success, or NULL.

Examples:

#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;
}

Classification:

Standard Unix

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

See also:

free(), malloc(), strcpy(), strncpy()


[Previous] [Contents] [Next]