strdup() function in c
strdup() is a C library function that dynamically allocates memory to create a new string identical to the input string. It copies each character from the source string until it reaches a null character, ensuring proper termination. The function handles memory allocation internally using malloc() or similar functions, alleviating the need for manual memory management. The returned pointer points to the newly created string, allowing convenient usage. Remember to free the allocated memory with free() function when it is no longer needed to avoid memory leaks.
Parameters:
Const char* str: A pointer to the string you want to duplicate.
Return Type:
char*: A pointer to the newly allocated memory containing the duplicated string.
In other words, strdup() takes a pointer to a string (str) as input and returns a pointer (char*) to a newly allocated memory block that holds the duplicated string. The function dynamically allocates memory for the duplicated string, so the programmer must free this memory when it is no longer needed using the free() function.
Syntax for the strdup() function:
It has the following syntax:
char* strdup(const char* str);
Example 1:
Let's take a program to illustrate the usage of the strdup() function:
#include<stdio.h>
#include<string.h>
int main(){
char *original_string = "TutorialsAndExample";
char *duplicate_string_pointer;
// strdup() function will return the pointer which is pointing to the duplicate string
duplicate_string_pointer = strdup(original_string);
printf("The original string is %s \n",original_string);
printf("The duplicate string is %s \n",duplicate_string_pointer);
return 0;
}
Output:
![strdup() function in c](https://static.tutorialandexample.com/c/strdup-function-in-c1.png)
Explanation for the program:
- int main(): The program starts execution from the main() function, which returns an integer.
- char *original_string = "TutorialsAndExample";: It declares a pointer original_string and initializes it with the address of the string literal "TutorialsAndExample". This string will serve as the original string that we want to duplicate.
- char *duplicate_string_pointer;: It declares a pointer duplicate_string_pointer without initializing it. This pointer is used to store the address of the duplicated string.
- duplicate_string_pointer = strdup(original_string);: It calls the strdup() function with original_string as the argument. This function duplicates the original_string and returns a pointer to the duplicated string. The pointer is assigned to duplicate_string_pointer.
- printf("The original string is %s \n",original_string);: It prints the original string using the %s format specifier.
- printf("The duplicate string is %s \n",duplicate_string_pointer);: It prints the duplicated string using the %s format specifier. The duplicate_string_pointer holds the address of the duplicated string.
Example 2:
Let's take a program to demonstrate the other ways of copying the string or making a duplicate of a string:
#include<Stdio.h>
#include<string.h>
void copy_string_method1(char * original_string){
int original_string_length = strlen(original_string);
char duplicate_string[strlen(original_string)];
int i=0;
while(original_string[i]!='\0'){
duplicate_string[i]=original_string[i];
i++;
}
duplicate_string[i]='\0';
printf("duplcate string generated by using method1 is %s \n",duplicate_string);
}
void copy_string_method2(char * original_string){
int original_string_length = strlen(original_string);
char duplicate_string[strlen(original_string)];
strcpy(duplicate_string,original_string);
printf("duplcate string generated by using method2 is %s \n",duplicate_string);
}
void copy_string_method3(char *original_string){
int original_string_length = strlen(original_string);
char duplicate_string[strlen(original_string)];
char* dupicate_string_pointer=duplicate_string;
while(*original_string !='\0'){
*dupicate_string_pointer = *original_string;
original_string ++;
dupicate_string_pointer ++;
}
*dupicate_string_pointer='\0';
printf("duplcate string generated by using method2 is %s \n",duplicate_string);
}
int main(){
char *original_string = "TutorialsAndExample";
copy_string_method1(original_string);
copy_string_method2(original_string);
copy_string_method3(original_string);
}
Output:
![strdup() function in c](https://static.tutorialandexample.com/c/strdup-function-in-c2.png)
Explanation of the above program:
copy_string_method 1:
- This method takes a pointer to the original string as input.
- It calculates the length of the original string using strlen().
- It creates an array duplicate_string with the same length as the original string.
- Using a loop, it copies each character from the original string to the corresponding position in the duplicate string.
- Finally, it adds a null character ('\0') at the end of the duplicate string to ensure proper termination.
- The duplicated string is printed using printf().
copy_string_method 2:
- This method takes a pointer to the original string as input.
- It calculates the length of the original string using strlen().
- It creates an array duplicate_string with the same length as the original string.
- It uses the strcpy() function to copy the content of the original string to the duplicate string.
- The duplicated string is printed using printf().
copy_string_method 3:
- This method takes a pointer to the original string as input.
- It calculates the length of the original string using strlen().
- It creates an array duplicate_string with the same length as the original string.
- It also creates a pointer duplicate_string_pointer pointing to the start of the duplicate string.
- Using a loop, it copies each character from the original string to the corresponding position in the duplicate string by dereferencing the pointers.
- Finally, it includes a null character ('\0') at the end of the duplicate string to ensure proper termination.
- The duplicated string is printed using printf().
In the main() function:
The original string is declared and assigned the value "TutorialsAndExample". Each copy method is called and passes the original string as an argument.