首页 文章

格式的参数太多[-Wformat-extra-args]

提问于
浏览
1

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

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

还有另一个警告:

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

我不知道该怎么办

1 回答

  • 5

    你的 fprintf 调用有8个格式说明符,但是传递了9个其他参数来填充它们 .

    第8格式说明符是 %d ;对应于此的参数是 Item[i]->Name . 警告告诉您 Item[i]->Name 是一个字符串,因此可以将't (shouldn' t)转换为有符号整数 .

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

相关问题