1 #include<iostream>
 2 #include <assert.h>
 3 using namespace std;
 4 
 5 void myStrcpy(char* Dest, const char *Src)
 6 {
 7     assert((Dest!= NULL)&&(Src!=NULL));
 8     while((*Dest = *Src)!='\0')
 9     {
10         Dest++;
11         Src++;
12     }
13 }
14 
15 int main()
16 {
17     char dest[] = "helloworld";//注意
18     char* src = "hello";
19     myStrcpy(dest, src);
20     printf("%s",dest);
21     return 0;
22 }

 

 

注意:在第17行中,我们用的是字符数组,因为如果用字符指针的话,字符常量存放在常量区,指针会指向这个地址,不能通过修改指针指向内容修改字符串。如果用字符数组的话,会把“helloworld“内容复制到数组中去,可以被修改。

相关文章:

  • 2021-04-04
  • 2021-07-14
  • 2021-08-04
  • 2021-07-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
相关资源
相似解决方案