我收到此错误:
warning: format ‘%f’ expects type ‘float *’, but argument 4 has type ‘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;
}
}
最佳答案
如果您读过例如this scanf
(and family) reference ,您将看到格式中的星号用于禁止写入任何内容。
因此,您有一种格式可以跳过一个字符,然后读取一个整数和三个浮点值。但是,您向函数传递了五个 个参数,最后一个将被忽略。
我假设您只想传递四个参数以匹配格式。
<小时/>只是推测,但如果您使用第一种格式("%*c"
)来跳过换行符(或任何其他空格),则链接的引用也应该告诉您它不是需要时,"%d"
格式说明符将跳过所有前导空白(包括空格、制表符和换行符)。
关于c - 警告 : format ‘%f’ expects type ‘float *’ , 但参数 4 的类型为 ‘int *’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32181255/