Strings are an important part of programming because they are used to send messages to the user, display the results, read the files or specify their path, among other things. They are also the input and output method that facilitates the interaction between the user and the program.
In this Linux Hint article, you will learn how to use the strdup() function to duplicate the strings. We will see the syntax and the theoretical description of calling the method, its input and output arguments, and the accepted data type in each case.
Then, we apply what we learned in the practical examples using code snippets and images to show you how to use the strdup() function to duplicate the strings.
Syntax of the Strdup() Function in C language
Description of the Strdup() Function in C language
The strdup() function duplicates a string. This function duplicates the string that is pointed to by the str pointer which is passed in the input argument, and returns a pointer to the new string as the result.
Strdup() belongs to the strXXdup() family of functions, all of which are intended to duplicate the strings, but each of which has a different calling mode and functionality to meet the particular needs of the programmer.
Like all string processing functions, strdup() is defined in the “string.h” header. To use it, you must include it in your code as follows:
How to Duplicate a String Using the Strdup() Function in the C Language
In this example, we use the strdup() function to duplicate a string. Then, we use the printf() function to display the “original” and “duplicate” string pointers in the command console.
To do this, we include the “stdio.h” and “string.h” headers in our code and define the two string pointers inside the main() function. The first one is called “original” which contains the “Example for the strdup() function” expression. The second pointer is called “duplicate” which contains the location where the strdup() function stores the copy of the original string.
Once the strings that the program uses are defined, we call the strdup() function and pass the pointer to the original string as the input argument and the pointer to the duplicated string as the output argument. Then, we use the print() function to display the “original” and the “duplicate” string pointers on the command console. Here is the code for this example:
#include <string.h>
void main ()
{
char* original ="example of strdup() function";
char* duplicate;
duplicate = strdup (original);
printf ("Original :%s\n", original);
printf ("Duplicate :%s\n", duplicate);
}
As we see in the following figure, strdup() duplicates the original string by literally duplicating its contents.
Differences Between the Strdup() and Strndup() Functions
The strXXdupX() family consists of the following functions:
char *strndup(const char *s, size_t n);
char *strdupa(const char *s);
char *strndupa(const char *s, size_t n);
The functions in this family are all designed to duplicate the strings, but each of them works in a different way.
As we have seen, the strdup() function completely duplicates the string that is sent in its input argument and returns an exact copy in its output argument.
The strndup() function duplicates the string of its input argument. It can do so completely or partially.
Unlike strdup(), the strndup() function takes two input arguments. The first argument is “str” which is the pointer to the string to be duplicated. The second argument is “n” which is a variable of type size_t that specifies the number of characters that it copies.
In the following example, we see the use of the strndup() function where only 8 of the characters of the original string are duplicated.
#include <string.h>
void main ()
{
char* original ="example of strdup() function";
char* duplicate;
size_t n =8;
duplicate = strndup (original, n);
printf ("Original :%s\n", original);
printf ("Duplicate :%s\n", duplicate);
}
As seen in the following figure, the strndup() function duplicates a number “n” characters from the original string.
Errors That Can be Generated by the Strdup() Function
The error that this function can generate is that “the system does not have enough memory to run the process” which is unlikely on current systems.
This error is defined in the “errno.h” header and its code can be queried via the errno global variable. Its definition is ENOMEM.
Conclusion
In this Linux Hint article, we explained how to use the strdup() function which belongs to the family of string processing functions that is defined in the string.h header.
We looked at the syntax of this function and provided a theoretical explanation about its usage, its input and output arguments.
To help you better understand how it works, we created a practical example with code and images where you can see how the pointers and strings that are used by this function are created and how the function is called.
We also showed you how to use the strndup() function, another function from the strXXdupX() family, so that you know the different possibilities that the C language offers to duplicate the strings.