指针实现字符串连接:
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 void my_strcat(char *dest,char *src)
5 {
6 int i=0;
7 while(*(dest+i))
8 {
9 i++;
10 }
11 int j=0;
12 while(*(src+j))
13 {
14 *(dest+i+j)=*(src+j);
15 j++;
16 }
17 *(dest+i+j)='\0';
18 }
19 int main(int argc, const char *argv[])
20 {
21 printf("请输入一串字符:\n");
22 char dest[50]="";
23 char src[50]="";
24 scanf("%s",dest);
25 printf("请输入要连接的字符:\n");
26 scanf("%s",src);
27 my_strcat(dest,src);
28 printf("连接后的结果为:%s\n",dest);
29 return 0;
30 }
错误:string这个一维字符数组里只能装9个单字符和一个\0,而指针str1指向的是含有10个单字符的字符串,装不下这么多字符,会越界存储。
错误: 单字符a不能用拷贝函数进行拷贝。
错误:给指针s指向的AAA存储在只读区,不能进行修改。
错误:
*MyData是野指针,不能使用,Sum应该赋初值为0,不然就会是随机值,没法正确累加。i应该从1开始,<101或者<=100,上述描述只能从0加到99,第一个循环的指针MyData不能直接等于i,不然不是首地址,导致第二个循环没有意义。
XMind:
上课老师的板书: