In this screen, we can see the storage of variable NvSt_InfoB.u8_PumpDuty,
It is a byte value which has 8bits, I want to change its value in a routine, so I transfer its address into the routine, but the address used by the routine should be int32, so I force it address to int32 by do this: (INT32U*)&NvSt_InfoB.u8_PumpDuty.
All of the process done are still right, but later its problem coming. When I get the value which stored in the 32 bits address, it get the value of
|
u8_PumpDrv[0](0) |
u16_PumpCycle(110:0x6E) |
NvSt_InfoB.u8_PumpDuty(50:0x32) |
So the value is 6E32 (28210)
Form doing this we can know two things
what the internal process of a forceful variable type change?
Answer: it will extend the higher address, and lower address store the low byte value.
So now we used little-ended.
Attention: you can forceful change a variable from Int8 to Int32
,but if you want to forceful change a pointer from Int8* to Int32* you need to know that the value store in the Int32* address maybe different, its value related with the 24bits value near the 8bits address, if these 24bits not 0 , you will get a wrong value finally!
强制类型转换在变量中可能好用,但在转化指针时就要注意了,这个转换后的地址取出的值可比转换前的长或者短,这就出问题了。
强制类型转换问题解析
本文探讨了在将8位变量地址强制转换为32位地址过程中遇到的问题,并详细解释了小端模式下高低字节的存储方式。通过实例分析,揭示了在不同位宽间进行强制类型转换时需要注意的事项。
2522

被折叠的 条评论
为什么被折叠?



