我将变量display_address,version,serial_no []定义为uint8_t并将其转发给函数,该变量需要为指针,因此我将其强制转换为(uint8_t *)变量,此后,我得到警告
警告:从较小的整数类型uint8_t(又名“ unsigned char”)转换为“ uint8_t *”(又名“ unsigned char *”)
有什么问题,为什么这行不通?
这是我要转发参数的函数
void write_to_flash (void) {
BYTE i;
iap_copy_to_flash((uint8_t *)display_address, OFFSET_ADDRESS, 1); //warning here
delay_1_ms();
iap_copy_to_flash((uint8_t *)version, OFFSET_VERSION, 1); //warning here
delay_1_ms();
for (i=0;i<8;i++) {
iap_copy_to_flash((uint8_t *)serial_no[i], OFFSET_VERSION+i, 1); //warning here
delay_1_ms();
}
}
Run Code Online (Sandbox Code Playgroud)
和iap_copy_to_flash参数说明
void iap_copy_to_flash (uint8_t* buff, uint32_t flash_addr, uint32_t num_bytes)
Run Code Online (Sandbox Code Playgroud)
您正在将整数转换为指针,这意味着您将实际值display_address作为指针位置传递。除非这是某种描述的循环转换,否则这是99%的时间是一个糟糕的主意™,但是我建议改为固定中间存储类型。
相反,您应该使用&运算符来获取变量的地址:
void write_to_flash (void) {
BYTE i;
iap_copy_to_flash(&display_address, OFFSET_ADDRESS, 1);
delay_1_ms();
iap_copy_to_flash(&version, OFFSET_VERSION, 1);
delay_1_ms();
for (i=0;i<8;i++) {
iap_copy_to_flash(&serial_no[i], OFFSET_VERSION+i, 1);
delay_1_ms();
}
}
Run Code Online (Sandbox Code Playgroud)