【发布时间】:2021-10-26 05:55:06
【问题描述】:
struct A
{
uint8_t hello[3];
};
struct B
{
const struct C* hello;
};
struct C
{
uint8_t hi[3];
};
B.hello = &reinterpret_cast<C &>(A);
假设我已经用值1、2、3 填充了结构A。
如果我打印B.hello.hi[0],我会得到0。相反,我应该得到1。
我是不是选错了?
我检查了代码中reinterpret_cast 行正上方的struct A 的值,它打印正常,所以我认为将值存储在A 中没有任何问题。这只是导致问题的转换。
【问题讨论】:
-
相反,我应该得到 1 - 为什么?看看
reinterpret_cast可以做的list of valid conversions 并思考哪个项目符号适用于您正在尝试做的事情。
标签: c++ reinterpret-cast