如何解决这个编译器错误'格式不是字符串文字和没有格式参数' [英] How to fix this compiler error 'format not a string literal and no format arguments'
问题描述
我有编译器错误:
I have the compiler error:
libvncserver/main.c:245: error: format not a string literal and no format arguments
第245行是:
And line 245 is:
fprintf(stderr,buf);
其中buf是char buf [256];
where buf is "char buf[256];"
我没有看到245行有什么问题,我该如何解决?
当我注释掉那行代码时,程序就会编译。
I don't see what is wrong with line 245 and how can I fix it? When I comment out that line, the program compiles.
推荐答案
有什么问题是,任何<$ c $当你提供一个 char *
函数时,printf 函数需要一个 const char *
。由于缓冲区可以包含任何你想要的东西,编译器不能确定它将包含正确的格式字符串。只要做
What is wrong is that any printf
function expects a const char *
while you are providing just a char *
. Since the buffer can contain whatever you want the compiler is not sure that it will contain a correct format string. Just do
fprintf(stderr,"%s",buf);
这样就可以确保你不会传递奇怪的东西。
so that it will be sure that you are not going to pass something strange.
这篇关于如何解决这个编译器错误'格式不是字符串文字和没有格式参数'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!