【问题标题】:WARNING in c called " 's1' and 's2' is used uninitialized in this function" [duplicate]c中的警告称为“在此函数中未初始化使用's1'和's2'”[重复]
【发布时间】:2020-05-28 13:02:26
【问题描述】:

无法解决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;
}

【问题讨论】:

    标签: c warnings


    【解决方案1】:

    你必须为s1s2分配:

    // 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函数中的,应该改成(Disadvantages of 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]; 
    

    【讨论】:

    • 谢谢,我通过添加 *s1=NULL 让它工作了,在这种情况下应该初始化一个指针
    • @AhmadIstaitih 欢迎您。
    猜你喜欢
    • 2020-01-07
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2013-05-12
    • 2015-12-30
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多