c - 如何在 C 中处理一个 float 以写入 4 个单独的字节?

标签 c pointers compiler-warnings

我有一个需要 32 位 float 的函数,但是在某种模式下,它期望 float 实际上只是 4 个 UINT8 组合在一起。

我编写的代码有效,但是我收到编译器警告“非法指针”警告,我想知道最“正确”的方法是什么?代码的可读性也非常重要,我认为我写的东西很容易阅读,尽管可能有一些方法可以用更少的行来编写它。

UINT8 time[6];
FLOAT FSTtime = 0;    //32bit float
UINT8 * FSTtimePointer;

//Get the current time as 6 bytes {secs,mins,hours,day,month,year}
returnSize = appP->DB_get_param_value(12, 0, 8, &time);
if (returnSize < 6) return;

(UINT8*)FSTtimePointer = &FSTtime;   //Line 876

//Pack 4 bytes of time data into a "float"
FSTtimePointer [0] = time[3];  //Reverse month & day because americans are backwards
FSTtimePointer [1] = time[4];
FSTtimePointer [2] = time[2];
FSTtimePointer [3] = time[1];

appP->HIST_write_FST_log(SubMin_CFG.OFFSET, (UINT16)historyPointer, FSTtime);

HIST_write_FST_log 的原型(prototype)是

  void   (*HIST_write_FST_log) (UINT8 hist_pt_index, UINT16 log_index, FLOAT value);

我得到的编译器警告是:

876: C1000 (W) Illegal pointer assignment
876: C1024 (W) First operand of "=" is not lvalue

如果有人感兴趣,我使用的编译器是用于日立处理器的 Renesas H8。

最佳答案

您可以使用修改代码如下

FSTtimePointer = (UINT8*) &FSTtime;

关于c - 如何在 C 中处理一个 float 以写入 4 个单独的字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36074096/

相关文章:

c++ - 强制缩小转换警告

c - open() 和 read() 系统调用...程序未执行

在不使用指针的情况下将数组转换为多维 C

c++ - 让一个对象包含另一个相同类型的对象?

java - 在 lambda 形状分析期间检测到内部不一致

objective-c - 强制子类在 Objective-C 中调用其父类(super class)方法

c - #QNANO 在 C 中表示或表示什么?

c - 我的 memcpy 实现失败

c - 如何更改 C 中二维数组的内容?

将整数转换为另一个字符指针