解决 C 语言中的 'format '%d' expects argument of type 'int *', but argument 3 has type 'uint8_t *'' 错误

如果您在使用 sscanf 函数解析字符串时遇到 'format '%d' expects argument of type 'int *', but argument 3 has type 'uint8_t *'' 错误,这意味着您使用了错误的格式说明符来读取 uint8_t 类型的变量。

错误原因:

您尝试使用 %d 格式说明符将字符串转换为十进制整数 (int),并将其存储到 uint8_t 类型的变量中。%d 需要一个指向 int 类型的指针,而您提供的是指向 uint8_t 类型的指针,这会导致类型不匹配。

解决方法:

要解决此错误,请使用 %hhu 格式说明符,该说明符专用于读取无符号字符类型 (unsigned char),而 uint8_t 通常是 unsigned char 的别名。

示例:

在您的代码中,您需要将以下两行:csscanf(token, '%d', &device_send.remote.signal);sscanf(token, '%d', &device_send.remote.bat);

修改为:csscanf(token, '%hhu', &device_send.remote.signal);sscanf(token, '%hhu', &device_send.remote.bat);

其他建议:

  • 编译器还提示 'uart_rx_data' 函数定义了但未被使用。如果您不再需要此函数,最好将其从代码中删除以提高代码整洁度。 * 仔细检查您的代码中其他使用 sscanf 的地方,确保使用了正确的格式说明符来匹配变量类型。

通过使用正确的格式说明符,您可以解决此错误并确保代码按预期工作。

解决 'format '%d' expects argument of type 'int *', but argument 3 has type 'uint8_t *'' 错误

原文地址: https://www.cveoy.top/t/topic/bBMc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录