英文:
Variable and pointer type casting warnings
问题
我最近为一个微控制器更新了一个新的编译器,这个新的编译器给了我以下的警告(参见图片)。旧的编译器编译时没有警告。
首先,我已经很多年没有写程序了,也不是程序员。一个字符串被发送到一个i2c函数,以发送到LCD,问题中的函数是i2c_write_buff(const buffer_t, unsigned char); 它不喜欢我下面展示的第一个参数类型和函数原型。
// 缓冲区指针类型。缓冲区由ISR和主线程代码共享。
// 缓冲区的指针也由ISR和主线程代码共享。
// 因此,有双重的volatile限定符
typedef volatile unsigned char * volatile buffer_t;
// 仅写入一个字符串到从机
int i2c_write_buff(const buffer_t, unsigned char);
调用此函数的函数如下所示:
void lcd_string(const unsigned char *string)
{
i2c_write_buff(string, lcd);
}
LCD函数是为其他类型的微控制器单独编写的,因此在这个程序之外不知道typedef buffer_t。我正在尝试找出如何让它们匹配并让编译器满意,而不必将typedef添加到LCD库中。
我尝试了不同的类型,但编译器仍然不喜欢。
英文:
I recently updated to a new compiler for a microcontroller and this new compiler is giving me the following warnings (see image). Old compiler it compiled without warnings.
Preface, it has been many years since I wrote the program and I am not a programmer. A string is being sent to a i2c function to send to a lcd, the function in question is i2c_write_buff(const buffer_t, unsigned char); It doesn't like the first argument type which I show below and the function prototype.
// buffer pointer type. The buffer is shared by an ISR and mainline code.
// the pointer to the buffer is also shared by an ISR and mainline code.
// Hence the double volatile qualification
typedef volatile unsigned char * volatile buffer_t;
// write only, a string to the slave
int i2c_write_buff(const buffer_t, unsigned char);
The function that calls this function is shown below:
void lcd_string(const unsigned char *string)
{
i2c_write_buff(string, lcd);
}
The LCD function was written separately for other types of microcontrollers and so doesn't know of the typedef buffer_t outside of this one program. I'm trying to figure out how to get these to match and make the compiler happy without having to add the typedef to the lcd library.
I tried different types that the compiler still didn't like.
答案1
得分: 1
> 如何使这些匹配
去掉常量性。最简单的方法是强制转换为相同类型:
void lcd_string(const unsigned char *string) {
i2c_write_buff((const buffer_t)string, lcd);
}
再次强调,typedef 指针会造成混淆。const buffer_t
使指针成为常量,而非指向的元素。
英文:
> how to get these to match
Remove the constness. The simplest would be to cast to the same type:
void lcd_string(const unsigned char *string) {
i2c_write_buff((const buffer_t)string, lcd);
}
Once again, typedef pointers are confusing. const buffer_t
is making the pointer constant, not pointed-to elements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论