[英]sprintf annoying warning in Keil
I am trying to use sprintf();
我正在尝试使用sprintf();
function in Keil. 在Keil中发挥作用。 But I have annoying warning. 但是我有烦人的警告。 Let me explain my warning with example code part right below. 让我用下面的示例代码部分解释我的警告。 When i debug, i get; 当我调试时,我得到了;
warning: #167-D: argument of type "uint8_t *" is incompatible with parameter of type "char *restrict" 警告:#167-D:“ uint8_t *”类型的参数与“ char * restrict”类型的参数不兼容
And it warns me on the line about format types. 它警告我有关格式类型的信息。
I know sprintf function is not a good solution but i really wonder why this warning show off? 我知道sprintf函数不是一个好的解决方案,但是我真的很奇怪为什么这个警告会炫耀吗?
Thanks 谢谢
#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 );
}
Fixing your warning: 解决警告:
You have two options: either declare str
as char
, or use type casting: 您有两个选择:将str
声明为char
或使用类型转换:
sprintf((char *) str, "%9ld", s_value);
Optimizing your code: 优化代码:
The only reason you have a loop is to initialize str
array with zeroes. 出现循环的唯一原因是用零初始化str
数组。 Following is the code that does that in a simple, readable manner, without code overhead: 以下是以简单易读的方式执行此操作的代码,而没有代码开销:
char str[9] = {0};
Fixing your code: 修改您的代码:
Excerpt from documentation: 文档摘录:
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. 即使结果较大,该值也不会被截断。
This means your code eventually will get a buffer overflow and will crash. 这意味着您的代码最终将导致缓冲区溢出并崩溃。 Use snprintf
! 使用snprintf
!
uint8_t
and char
is not necessarily the same thing. uint8_t
和char
不一定是同一件事。 Usually actually it's typedef'd as unsigned char
. 通常,实际上将它定义为unsigned char
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.