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));