C语言 格式参数过多 [-Wformat-extra-args]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17091746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:41:26  来源:igfitidea点击:

too many arguments for format [-Wformat-extra-args]

c

提问by eduroam

Im doing a project for school and this is the warning that keeps bugging me. Whats wrong with my code?

我正在为学校做一个项目,这是一直困扰着我的警告。我的代码有什么问题?

fprintf(fp,"%s\n%s\n%s\n%s\n%s\n%s\n%s\n%d\n", Item[i]->ID, Item[i]->Date, Item[i]->Adress,
                                       Item[i]->Street number, Item[i]->Postal Code,
                                       Item[i]->City, Item[i]->Phone,Item[i]->Name,
                                       Item[i]->Price);

Also there is another warning:

还有另一个警告:

warning: format '%d' expects argument of type 'int', but argument 10 has type 'char *' [-Wformat]

警告:格式 '%d' 需要类型为 'int' 的参数,但参数 10 的类型为 'char *' [-Wformat]

I don't know what to do

我不知道该怎么办

回答by simonc

Your fprintfcall has 8 format specifiers but passes 9 further arguments to fill these.

您的fprintf调用有 8 个格式说明符,但传递了 9 个进一步的参数来填充这些。

The 8th format specifier is %d; the argument corresponding to this is Item[i]->Name. The warning is telling you that Item[i]->Nameis a string so can't (shouldn't) be converted to a signed integer.

第 8 个格式说明符是%d; 与此对应的参数是Item[i]->Name。警告告诉您这Item[i]->Name是一个字符串,因此不能(不应)转换为有符号整数。

I presume Item[i]->Pricehas type int; you then either need to add an extra %sto your format string (anywhere before the %d) or remove one of the string arguments.

我想Item[i]->Price有类型int; 然后,您需要%s在格式字符串中添加额外内容(在 之前的任何位置%d)或删除字符串参数之一。