您好,关于以下这个C语言 stacat的函数问题?该怎么解决?

#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);
}
编译出来后为什么后面会有烫烫烫烫烫。。。如图求解

jeck猫
浏览 289回答 2
2回答

森栏

最后的s1后面没赋值‘\0’修正后的#include<stdlib.h>#include<stdio.h>#include<conio.h>void&nbsp;fun(char&nbsp;p1[],char&nbsp;p2[]){char&nbsp;*s1,*s2;s1=p1;s2=p2;while(*s1!='\0')s1++;while(*s2!='\0'){*s1=*s2;s1++;s2++;}*s1='\0';}void&nbsp;main(){&nbsp;char&nbsp;s1[80],s2[40];&nbsp;system("CLS");&nbsp;printf("Enter&nbsp;s1&nbsp;and&nbsp;s2:\n");&nbsp;scanf("%s&nbsp;%s",s1,s2);&nbsp;printf("s1=%s\n",s1);&nbsp;printf("s2=%s\n",s2);&nbsp;printf("Invoke&nbsp;fun(s1,s2):\n");&nbsp;fun(s1,s2);&nbsp;printf("After&nbsp;invoking:\n");&nbsp;printf("%s\n",s1);}

白猪掌柜的

因为S1没有'\0'结束符,在while(*s1!='\0')s1++;while(*s2!='\0'){*s1=*s2;s1++;s2++;}两个地方,都把'\0'去掉了,在fun函数最后加s++;*s='\0'
打开App,查看更多内容
随时随地看视频慕课网APP