我有一个程序从键盘读取 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 ) {
;
}
}
最佳答案
scanf("%79s%79s", string1, string2);
这解决了我的问题。或者可以使用:
scanf_s("%79s %79s", string1,80, string2, 80);
关于c - 从键盘读取两个字符串导致程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22738916/