忽略'scanf'的返回值,使用属性warn_unused_result声明

问题描述 投票:0回答:1

所以我有这个功能,基本上是scanf一个人将要编写并返回的名称。

但是,当我检查错误时,它表示我实际上返回它并在其他地方使用时,忽略了scanf的值。

char* nomes (int n, char* nome ) {
    if (n == 1) printf(" Escolha o nome \n(max10 e sem espaços)\n");
    printf(" Jogador %d:",n);
    scanf("%10s",nome);
    print_linha ();
    return nome;
}
c gcc-warning
1个回答
0
投票

仅查看scanf功能,例如here。返回int。您在代码中所做的就是使用由scanf修改的参数。这是您想要的:

char* nomes (int n,char* nome ){
    if (n==1) 
        printf(" Escolha o nome \n(max10 e sem espaços)\n");
    printf(" Jogador %d:",n);

    int result = scanf("%10s",none);
    // do something with result
    ...

    print_linha ();
    return nome;
}

如果您不需要返回类型,可以简单地将其强制转换(但是我认为这是一种不好的做法):(void)scanf("%10s",none);

© www.soinside.com 2019 - 2024. All rights reserved.