【问题标题】:Error passing a char* to printf() call将 char* 传递给 printf() 调用时出错
【发布时间】:2016-06-05 15:22:31
【问题描述】:

/***** 读取用户输入的条目 */

int input_string(char *prompt, char *s, int count) 
{
    char c;

    printf(prompt);
    while ((c = getchar()) != EOF 
           && c != '\n' && count > 0) {
        *s = c;
        s++;
        count--;
    }
    *s = '\0';
    return 0;
}

我是 C 编程新手。我正在浏览一个简单的数据库程序的示例,并且遇到了此代码 print(prompt); ,我的编译器给出了这个错误,

格式字符串不是字符串文字(可能不安全)[-werror, -wformat-security]。

请帮助停止错误。

【问题讨论】:

    标签: c printf


    【解决方案1】:

    和你的代码一样,

     printf(prompt);
    

    不好,因为它为string formatting attacks 提供了服务。您可以将其替换为

    puts(prompt);
    

    如果您不打算在输出中使用任何转换说明符(和相关参数)。

    【讨论】:

    • 谢谢 Sourav,会试试的。
    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 2010-11-19
    • 2016-05-27
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多