Function name : strdup
Header file : <string.h>
Function prototype: char *strdup(const char *str);
Function : Copy the string to the newly created space. This function will first use malloc() to configure the same space size as the parameter str string, then copy the content of the parameter str string to the memory address, and then return the address . This address can finally be released using free().
Parameters : char *str is the string to be copied
Return value : Returns a string pointer, which points to the new string address after copying. If NULL is returned, it indicates insufficient memory.
Note : It is not a standard library function and can only be used under windows (VC, MinGW, etc.). It needs to be defined by yourself in Linux GCC.
Program example: Copy the string string to dup_str and output the result
#include<string.h>#include<stdio.h>#include<stdlib.h>intmain(void){char*dup_str,*string=www.dotcpp.com;dup_str=strdup(string);printf(%s n,dup_str);free(dup_str);return0;}
Running results:
www.dotcpp.com