Implementation of the System function C string (5): StrDup

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.