strdup
From cppreference.com
< c | experimental | dynamic
Merged into ISO C The functionality described on this page was merged into the mainline ISO C standard as of 6/2019, see strdup (since C23)
Defined in header
<string.h>
char * strdup( const char *str1 );
(dynamic memory TR)
Returns a pointer to a null-terminated byte string, which is a duplicate
of the string pointed to by str1
. The returned pointer must be passed to free to avoid a memory leak.
If an error occurs, a null pointer is returned and errno may be set.
As all functions from Dynamic Memory TR, strdup
is only guaranteed to be available if __STDC_ALLOC_LIB__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT2__ to the integer constant 1 before including string.h
.
Contents
[edit] Parameters
str1
-
pointer to the null-terminated byte string to duplicate
[edit] Return value
A pointer to the newly allocated string, or a null pointer if an error occurred.
[edit] Notes
The function is identical to the POSIX strdup.
[edit] Example
Run this code