As the name indicates, the word “strdup” consists of two words: “string” and “duplicate.” Both these words combine to form a strdup. The meaning clearly depicts the function is used to duplicate the content of one string onto another. Like strdup, there exists another built-in function strndup. This works like strdup but duplicates the “n” amount of the given data. We will not use the “copy” word for this function, as copying data is the functionality of another built-in feature of strings in C that is strcpy. We will also discuss the difference between them later in this article. To understand the working, we need to understand the syntax.
char* strdup(const char* src);
The same goes on how the library is used, as previously mentioned. Then, when we consider the main syntax, we will see that a returning argument is used that is a character. This function returns the value/ pointer to a null-terminated string. As the pointer is a character, we have used “char” instead of any other data type. Then in the parameter of the strdup function, we have used a constant pointer of the string that is to be duplicated. This phenomenon will be understandable through the examples provided.
With the name of the pointer, we came across its function of storing addresses. So, this use of pointer is associated with the memory allocation in the same way.
Before going through the examples, we will see some useful descriptions regarding strdup on the manual of Linux. As we are going to implement it in a Linux environment, we must have the know-how of this feature. Go to the terminal and simply use the following command:
This function will lead you to the manual page. This page contains all the types and functionalities of strdup, along with the syntax for each type:
To implement the respective function, we have used a text editor. We write codes in the editors and execute the results on the Linux terminal. Now, consider the code. First, we have used the string library in the header file:
In the main program, we have used a single piece of a string that contains a line:
To duplicate the string, we first need to take another pointer-type variable. Here, it is named as “target.” And then, we will use the strdup function to copy the string:
And then, we will take the print of the target. The use of strdup is quite simple, like other string functions. Save the code with the “c” extension. Now, go to the terminal, and then we will use a compiler to compile and then execute the code of that file. So for the C programming language, we will use the “GCC” compiler:
You will see the resultant value that is the name as we have written in the input file. The “-o” is used to save the results in a file and bring it to the screen from there.
The second example is the use of strndup. As discussed, it duplicates the value of a string up to some extent that is mentioned in the function. Following the same approach, use the string library and initialize a string with an input value. A new dynamic memory is allocated, and with the help of a pointer, all the values are duplicated in the second string. We will duplicate 7 characters from the input string to the second one:
By using this, you will see that only the first 7 bytes are considered, and their content is displayed. See the results in the terminal using the GCC compiler:
You can see that the first 7 values are displayed in the result.
Difference Between Strdup and Strcpy
Using this function, you must have wondered if the id strdup() duplicates the input string and strcpy() copies the input string, what is the difference?
The answer to this question can be found in the implementation of both features. When we use the strcpy function:
In this function, we copy the data from the source file to the destination file. Whereas in the case of the strdup() function, we need to allocate and then de-allocate the memory with the destination. This difference occurs because the strcpy function only copies the data of one file to other; there is no need to specify a particular space in the memory. Now, we will use a simple code for strcpy to demonstrate the concept of its usage with strdup:
while (*dest++ = *sorc++);
This statement shows that the parameter contains both the source and destination items as an argument. We can use strcpy for both the static and dynamic memory allocation. Because at run time, a specific file is chosen in a particular memory space. So this demonstration shows the usage of strcpy in terms of strdup.
In the case of strdup, we use a specific function, malloc(), to allocate the memory dynamically. But it is favorable if you delete the content or free the space after usage. So for this purpose, simply use strdup() with malloc(), and then copy the source string to the allocated memory.
Another feature that is used in addition to strdup is “memcpy”. This is used to increase the speed of duplicating the string from the source compared to strcpy.
In this example, a function is used having strdup with arguments of the input string as the source. Strlen is a string feature used to take the total length of a string. After that, a pointer variable is used in which the string is to be duplicated through malloc. The system first checks the variable to be empty through an “if-statement”. If the space is free, then the value should be duplicated there. The memcpy function takes input, output, and the length too for its speedy purpose:
The main program accepts the string we want to provide. Then, the function is called for the value to be stored in the output value. Both the input and output values are displayed. In the end, the utilized space is free:
This article “Strdup function usage” is implemented in C programming language to demonstrate the working and its usage with the help of elementary examples. We have quoted separate examples for strdup and strndup. By reading this article, you will be able to discriminate between strdup and strcpy as each function is explained with examples to differentiate their usage. We hope this effort will be sufficient to lead the access of C programming language in an aspect of string functions. Check other Linux Hint articles for more tips and information.