StrDup does not need to allocate memory space, automatically implement the copy of the string
Char *mystrdup (char *str) {char*p = malloc (sizeof (char) * (strlen (str) + 1)); strcpy (P, str);//Copy return p;} /* Function Name: StrDup : Copy the string to the new location using the method: Char *strdup (char *str); *///strcpy variant void Main () {char str[14] = "rocket-night \ n" ; str[0] = ' R ';p rintf ("str=%s\n\n", str), char *p1 = Null;char *p2 = "Calc";p 1 = mystrdup (p2);p rintf ("p1=%s p2=%s\n\n", P1, p2);p 1 = _strdup (str);p rintf ("p1=%s", p1); free (p1); System ("pause");
Copyright NOTICE: This article is for bloggers original article, welcome to point out the code is bad, put forward the Code optimization scheme. Welcome guidance, Night code, desperately updated, hard struggle ...
Implementation of the System function C string (5): StrDup