简体   繁体   English

将char *传递给printf()调用时出错

[英]Error passing a char* to printf() call

/***** reads user's input for an entry */ / *****读取用户输入的条目* /

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

Am new to c programming. 是c编程的新手。 I was going through an example of a simple database program & I met this code print(prompt); 我正在看一个简单的数据库程序的例子,遇到了这个代码print(prompt); , my compiler gives this error about it, ,我的编译器给出了与此有关的错误,

format string is not a string literal(potentially insecure) [-werror, -wformat-security]. 格式字符串不是字符串文字(可能不安全)[-werror,-wformat-security]。

Kindly help in stopping the error. 请帮助停止错误。

As in yout code, 就像您的代码中一样,

 printf(prompt);

is bad , as it open up provision for string formatting attacks . 这是不好的 ,因为它开放了字符串格式化攻击的规定。 You can replace that with 您可以将其替换为

puts(prompt);

in case you don't intend to use any conversion specifier (and related arguments) in your output. 如果您不想在输出中使用任何转换说明符(及相关参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM