From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/string%2Fbyte%2Fstrdup.html | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 devdocs/c/string%2Fbyte%2Fstrdup.html (limited to 'devdocs/c/string%2Fbyte%2Fstrdup.html') diff --git a/devdocs/c/string%2Fbyte%2Fstrdup.html b/devdocs/c/string%2Fbyte%2Fstrdup.html new file mode 100644 index 00000000..9b2114c8 --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fstrdup.html @@ -0,0 +1,30 @@ +

strdup

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

#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+ 
+int main(void)
+{
+    const char *s1 = "Duplicate me!";
+    char *s2 = strdup(s1);
+    printf("s2 = \"%s\"\n", s2);
+    free(s2);
+}

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)
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/string/byte/strdup +

+
-- cgit v1.2.3