c - Keil 中的 sprintf 令人讨厌的警告

标签 c keil

我正在尝试在Keil中使用sprintf();函数。但我有一个烦人的警告。让我用下面的示例代码部分解释我的警告。当我调试时,我得到;

warning: #167-D: argument of type "uint8_t *" is incompatible with parameter of type "char *restrict"

它会在线警告我有关格式类型的信息。

我知道 sprintf 函数不是一个好的解决方案,但我真的想知道为什么会出现这个警告?

谢谢

#include "stm32l0xx.h"  // Device header
#include <stdio.h>

void LCD_show(uint32_t  s_value)

{

  uint8_t str[9], i;

  for ( i = 0; i < 9 ; i++) str[i] = 0;

  sprintf( str, "%9ld", s_value );

}

最佳答案

修正警告:

您有两个选择:将 str 声明为 char,或使用类型转换:

sprintf((char *) str, "%9ld", s_value);

优化您的代码:

产生循环的唯一原因是用零初始化 str 数组。以下代码以简单、可读的方式执行此操作,无需任何代码开销:

char str[9] = {0};

修复代码:

摘自文档:

A format specifier follows this prototype:
%[flags][width][.precision][length]specifier
...
Width:
Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

这意味着您的代码最终将出现缓冲区溢出并崩溃。使用snprintf!

关于c - Keil 中的 sprintf 令人讨厌的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44698984/

相关文章:

c - 在同一个 for 循环中递增和打印变量

c++ - iostream keil c++ 问题

debugging - 在Keil uVision 5中,如何在逐步通过调试器时使文本编辑器行保持最新状态?

c - 相同的名称,但在 c 中具有不同的 case 变量和函数名称

c - 制作链表时报错: Expected Expression Before 'struct' ,

c - 绕过 Nios II 处理器中的数据缓存

c - 引用 "C - Help understanding how to write a function within a function (list_map)"

c - Keil 中定义的 __wfi() 等特定于 Cortex 的函数在哪里?

c - Keil Arm 编译器 : Is there a way to hook in the same function for two interrupts without modifying the Interrupt Vector Table?

将变量转换为 int vs round() 函数