strdup
From cppreference.com
Null-terminated byte strings
Functions | |||||||||||||||||||||||||||||||||||||||||
Character manipulation | |||||||||||||||||||||||||||||||||||||||||
Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
String examination | |||||||||||||||||||||||||||||||||||||||||
Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)
(C11)
|
Defined in header
<string.h>
|
||
char
*
strdup
(
const
char
*
src
)
;
|
(since C23) | |
Returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by
src
. The space for the new string is obtained as if the
malloc
was invoked. The returned pointer must be passed to
free
to avoid a memory leak.
If an error occurs, a null pointer is returned and errno might be set.
Parameters
src | - | pointer to the null-terminated byte string to duplicate |
Return value
A pointer to the newly allocated string, or a null pointer if an error occurred.
Notes
The function is identical to the POSIX strdup .
Example
Run this code
Output:
s2 = "Duplicate me!"
See also
(C23)
|
allocates a copy of a string of specified size
(function) |
(C11)
|
copies one string to another
(function) |
allocates memory
(function) |
|
deallocates previously allocated memory
(function) |
Retrieved from "
https://en.cppreference.com/mwiki/index.php?title=c/string/byte/strdup&oldid=136177
"