【发布时间】:2014-03-30 00:48:18
【问题描述】:
我有一个程序从键盘读取 2 个字符串,而 string1 将被 string2 替换。问题是我按 Enter 后程序立即崩溃。每个人都可以解释我的程序有什么问题吗?谢谢!
#include<stdio.h>
#define SIZE 80
void mystery1( char *s1, const char *s2 );
int main( void)
{
char string1[ SIZE];
char string2[ SIZE ];
puts("Enter two strings: ");
scanf_s("%79s%79s",string1,string2); // this line makes program crashed
mystery1(string1, string2);
printf_s("%s", string1);
}
// What does this function do?
void mystery1(char *s1, const char *s2 )
{
while ( *s1 != '\0') {
++s1;
}
for( ; *s1 = *s2; ++s1, ++s2 ) {
;
}
}
【问题讨论】:
-
我认为你需要在 %79s 之间留一个空格
-
我也做了同样的事情,但还是崩溃了
-
神秘函数 concat 是两个字符串。
-
@David 该函数不会导致崩溃
-
我知道,但是在你的代码中你问“这个函数是做什么的?”