#include<stdlib.h>#include<stdio.h>#include<conio.h>void fun(char p1[],char p2[]){char *s1,*s2;s1=p1;s2=p2;while(*s1!='\0')s1++;while(*s2!='\0'){*s1=*s2;s1++;s2++;}}void main(){char s1[80],s2[40];system("CLS");printf("Enter s1 and s2:\n");scanf("%s %s",s1,s2);printf("s1=%s\n",s1);printf("s2=%s\n",s2);printf("Invoke fun(s1,s2):\n");fun(s1,s2);printf("After invoking:\n");printf("%s\n",s1);}编译出来后为什么后面会有烫烫烫烫烫。。。如图求解
2 回答
森栏
TA贡献1810条经验 获得超5个赞
最后的s1后面没赋值‘\0’
修正后的
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void fun(char p1[],char p2[])
{char *s1,*s2;
s1=p1;s2=p2;
while(*s1!='\0')
s1++;
while(*s2!='\0')
{*s1=*s2;s1++;s2++;}
*s1='\0';
}
void main()
{
char s1[80],s2[40];
system("CLS");
printf("Enter s1 and s2:\n");
scanf("%s %s",s1,s2);
printf("s1=%s\n",s1);
printf("s2=%s\n",s2);
printf("Invoke fun(s1,s2):\n");
fun(s1,s2);
printf("After invoking:\n");
printf("%s\n",s1);
}
白猪掌柜的
TA贡献1893条经验 获得超10个赞
因为S1没有'\0'结束符,在
while(*s1!='\0')
s1++;
while(*s2!='\0')
{*s1=*s2;s1++;s2++;}
两个地方,都把'\0'去掉了,在fun函数最后加
s++;*s='\0'
添加回答
举报
0/150
提交
取消