C + + face question string copy function Example _c language

Source: Internet
Author: User
Tags strlen

Copy Code code as follows:

#include <iostream>
using namespace Std;

String copy function
char * scpy (char *strdest, char *strsource)
{
_assert ((strdest!= NULL) && (strsource!=null));
char *d = strdest; Gets the current position of the Dest
char *s = strsource; Gets the current position of source
while ((*d++ = *s++)!= ' ")//not to the last, make a loop
{
}
*d = ' n '; Supplement the last One
return strdest;
}

int main ()
{
Char *strsource = "Hello,world";
Char *strdest = new Char[strlen (strsource) +1]; Note that the return length of the strlen function does not include ' yes ', so add 1
_assert (strdest!= NULL);
Char *strreturn = scpy (Strdest,strsource);
cout<< "parameter return value" <<strDest<<endl;
cout<< "function return value" <<strReturn<<endl;

No release operation should also be no problem, the main thread after exiting the system will reclaim resources
Delete Strsource,strdest,strreturn;
strsource = strdest = Strreturn = NULL;
return 0;
}

strcpy (STR1,STR2) function can copy content from str2 to str1, why do I need function return value? It should be convenient to implement chain expressions, such as:

int i_length = strlen (strcpy (STR1,STR2));

Related Article

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.