The strdup() and strndup() functions are used to duplicate a string.
Syntax : char *strdup(const char *s);
This function returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by s. The memory obtained is done dynamically using malloc and hence it can be freed using free().
It returns a pointer to the duplicated string s.
Below is the C implementation to show the use of strdup() function in C:
syntax: char *strndup(const char *s, size_t n);
This function is similar to strdup(), but copies at most n bytes.
Note: If s is longer than n, then only n bytes are copied, and a NULL (”) is added at the end.
Below is the C implementation to show the use of strndup() function in C:
Reference: Linux man(7)
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Functions in C/C++
- Nested functions in C
- Inline Functions in C++
- Macros vs Functions
- List in C++ | Set 2 (Some Useful Functions)
- Thread functions in C/C++
- Searching in a map using std::map functions in C++
- Pure Functions
- C | Functions | Question 10
- C | Functions | Question 2
- C | Functions | Question 3
- C | Functions | Question 4
- C | Functions | Question 8
- Functions that cannot be overloaded in C++
- C | Functions | Question 5