Implement strcpy of in C++ return a char* type of in depth analysis
- 2020-04-02 00:57:03
- OfStack
The code is as follows:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
char* strcpy(char *src_str, char *dest_str)
{
char* dest = dest_str;
if ((src_str == NULL)||(dest_str == NULL)) //Check pointer validity
{
throw "Invalid argument(s)"; //An exception is thrown
}
while((*dest_str++ = *src_str++) != '0') //Copy, including '/0' at the end
{
NULL;
}
return dest;
}
int _tmain(int argc, _TCHAR* argv[])
{
char src[] = "Hello,world!";
char des[13] = {0};
strcpy(src, des);
cout << des << endl;
return 0;
}