In this blog post, you will learn about the strdup in C with the help of programming examples.
strdup() in C:
The strdup function creates a copy of the string pointed to by src
. The space for the new string is allocated as if by a call to malloc.
It returns a pointer to a null-terminated string that must be passed to free to avoid a memory leak. If an error occurs, the strdup returns a null pointer.
Syntax strdup in C:
The strdup() declare in <string.h> header file. The following is the syntax of the strdup function in C.
char *strdup(const char *src);
(since C23)
strdup Parameters:
The strcoll() the function accepts the following parameters:
src
— pointer to the null-terminated byte string to duplicate.
strdup return value:
The strdup() function returns a pointer to the first character of the duplicate string. The returned pointer must be passed to free() function to deallocate the allocated memory. If no space can be allocated the strdup function returns a null pointer.
Example program to describe how to use strdup in C:
The following program illustrates the working of the strdup() function in the C language.
#include <string.h> #include <stdio.h> #include <stdlib.h> int main() { const char *s1 = "Aticleworld.com"; char *s2 = strdup(s1); if(s2 != NULL) { printf("s2 = %s\n", s2); // calling free() free(s2); } return 0; }
Output:
Aticleworld.com
How to implement your own strdup function?
Your compiler/standard (C23) library will likely have a very efficient and tailored implementation of the strdup() function. So if not required avoid creating your own version of the strdup function.
We can implement the strdup function in many ways. Here we are implementing strdup using the help of the memcpy() function.
Note: Below function only to understand the working of strdup(). A lot of scenarios are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard strdup() function (library function).
char *mystrdup(const char *src) { size_t len = strlen(src) + 1; // String plus '\0' char *dst = malloc(len); // Allocate space if(dst != NULL) { memcpy (dst, src, len); // Copy the block } // Return the new duplicate string return dst; }
Let’s test-own created strdup function.
#include <string.h> #include <stdio.h> #include <stdlib.h> char *mystrdup(const char *src) { size_t len = strlen(src) + 1; // String plus '\0' char *dst = malloc(len); // Allocate space if(dst != NULL) { memcpy (dst, src, len); // Copy the block } // Return the new duplicate string return dst; } int main() { const char *s1 = "Aticleworld.com"; char *s2 = mystrdup(s1); if(s2 != NULL) { printf("s2 = %s\n", s2); // calling free() free(s2); } return 0; }
Output:
Aticleworld.com
Recommended Post:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- strerror function in C.
- Use of strncmp function in C programming.
- Strcmp function in C programming.
- How to use strxfrm function in C programming.
- Use of memcmp function with example code.
- How to use memcpy and implement own.
- Implement own memmove in C.
- memmove vs memcpy.
- strcoll in C with example code.
- Implement vector in C.
- How to Use strncpy() and implement own strncpy().