无法解决C中称为S1的警告 s2 使用未初始化此函数
int main()
{
char *s1, *s2;
printf("Please enter string s1 and then s2:n");
scanf("%s %s", s1, s2);
printf("%s %s", *s1, *s2);
return 0;
}
您必须为s1
和s2
分配:
// do not forget to include the library <stdlib.h> for malloc function
s1 = malloc(20); // string length of s1 ups to 19;
if(!s1) {return -1;}
s2 = malloc(20) // // string length of s2 ups to 19 also;
if(!s2) {return -1;}
在scanf
函数中,应该更改为(scanf的缺点(:
scanf("%19s %19s", s1, s2); // or using fgets
或者您可以使用字符数组而不是指针:
char s1[20], s2[20];
// Or you can define a maximum length MAX_LEN, then using:
// char s1[MAX_LEN], s2[MAX_LEN];