首先来看一段C程序:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 void GetMem(char*& pstr){//注意必须要用指针的指针或者指针的引用。如果传本身,返回的已经是空悬指针了
 6     pstr=(char*)malloc(20);
 7 }
 8 
 9 int main(){
10     char* str;
11     GetMem(str);
12 
13     strcpy(str,"Hello");
14     strcat(str+3,"World");//这里加的数字只要不超过Hello的长度都是一样的效果,最终都会寻找到末尾的0,然后连接
15     printf("%s",str);//HelloWorld
16     return 0;
17 }

再看一段:

1 #include<stdio.h>
2 #include<string.h>
3 main()
4 {
5     char *p1="abc",str[50]="xyz";
6     strcpy(str+2,p1);
7     printf("%s\n",str);//xyabc,注意是从第2个位置开始覆盖的
8 }

特别要注意这两个函数的异同。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2021-10-13
  • 2021-06-06
  • 2021-12-12
猜你喜欢
  • 2022-12-23
  • 2021-09-01
  • 2021-12-22
  • 2021-12-18
  • 2021-07-15
  • 2021-07-06
  • 2022-12-23
相关资源
相似解决方案