首页 > 解决方案 > c中的警告称为“'s1'和's2'在此函数中未初始化”

问题描述

无法解决 c 中称为 s1 的警告 s2 is used uninitialized this function

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;
}

标签: cwarnings

解决方案


您必须为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;}

inscanf函数,应更改为(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]; 

推荐阅读