【问题标题】:warning: format ‘%f’ expects type ‘float *’, but argument 4 has type ‘int *’警告:格式“%f”需要类型“float *”,但参数 4 的类型为“int *”
【发布时间】:2015-08-24 11:30:58
【问题描述】:

我收到此错误:

警告:格式“%f”需要类型“float *”,但参数 4 具有类型 'int *'

在这一行:

temp = sscanf(data,"%*c %d %f %f %f",&uptime, &inputs,  &systemstatus.adc4, &voltage, &idle);

我不明白为什么会这样,有人可以向我解释一下吗?

我正在使用带有 make 的 GCC 编译器,

#include "main.h"

void processcmd(char *data) {
    char foo[300];
    int temp, temp2, temp3;
    _statusstruct tempstatus;
    int uptime;
    int inputs;
    float voltage;
    float idle;

    if (data[0] != 'S') { sprintf(foo,"> %s",data); menu_main_log(foo); }

    switch (data[0]) {
        case 'S':           
            temp = sscanf(data,"%*c %d %d %f %f %f %f %f %d %d",
                &tempstatus.uptime,&tempstatus.inputs, &tempstatus.adc0, &tempstatus.adc1,
                &tempstatus.adc2,  &tempstatus.adc3, &tempstatus.voltage, &tempstatus.idle,
                &tempstatus.debug);
            if (tempstatus.uptime < systemstatus.uptime) {
                // big trouble: IO card rebooted.
                state = STATE_RESET;
                systemstatus.uptime = 0;
                printf("Detected IO reboot\n");
                break;
            } else if (temp == 9) {     
                memcpy(&systemstatus,&tempstatus,((char*)&tempstatus.newdata) - ((char*)&tempstatus));
                for (temp = 0; temp < 16; temp++) {
                    if (systemstatus.inputs & (1<<temp)) buttons[temp][2] = 1;
                    else buttons[temp][2] = 0;
                }
                systemstatus.newdata = 1;
            } else {
                printf("Status line was invalid (%d)\n", temp);
            }
            break;
        case 'I':           
            temp = sscanf(data,"%*c %d %d", &temp2, &temp3);
            if (temp == 2) {
                if (temp2 >= 0 && temp2 <= 15 && (temp3 == 0 || temp3 == 1)) {
                    buttons[temp2][temp3] = 1; 
                }
                buttons[temp2][2] = temp3;      // current state
                if (temp2 >= 3 && temp2 <= 6  && temp3 == 1) buzzer_on(5000,200);
            }
            break;
        case 'E':           // error report. rest of the line is the text

            break;
        case 'O':           // reply on output status set/request

            break;
        case 'T':           // reply on heater setting set/request

            break;
        case 'C':           // reply on calibration command

            break;
        case 'H':           // reply on calibration command

            temp = sscanf(data,"%*c %d %f %f %f",&uptime, &inputs,  &systemstatus.adc4, &voltage, &idle);

            break;

        default:
            printf("Unknown command: %s\n",data);
            break;
    }
}

【问题讨论】:

  • voltage 的类型是什么?
  • 该消息应该是不言自明的。它告诉您编译器的预期和实际传递的内容。
  • 看来你声明的是int inputs;而不是float inputs;
  • 只是我,还是论据4 inputs,而不是voltage
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建minimal reproducible example

标签: c


【解决方案1】:

如果您阅读例如this scanf (and family) reference,您会看到格式中的星号用于抑制写入任何内容。

所以你的格式会跳过一个字符,然后读取一个整数和三个浮点值。但是,您将 五个 参数传递给函数,最后一个将被忽略。

我假设您只想传递四个参数以匹配格式。


只是推测,但如果您有第一种格式 ("%*c") 来跳过换行符(或任何其他空格),则链接的参考也应该告诉您不需要它,"%d" 格式说明符将跳过所有前导空格(包括空格、制表符和换行符)。

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 2013-04-22
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多