我有这个结构:
t_scene *scene
我把它扔进了无效*
void *base = (void *)scene;
手动移动到一个字段(由于某些原因,手动移动它是必要的)
void *param = base + sizeof(int);
现在,如果我想取消引用它,我需要将参数转换为 int *:
*(int *)param = 12;
我有办法通过字节值取消引用吗?像这样的东西:
*(sizeof(int)*)param = 12;
预先感谢 !
我有这个结构:
t_scene *scene
我把它扔进了无效*
void *base = (void *)scene;
手动移动到一个字段(由于某些原因,手动移动它是必要的)
void *param = base + sizeof(int);
现在,如果我想取消引用它,我需要将参数转换为 int *:
*(int *)param = 12;
我有办法通过字节值取消引用吗?像这样的东西:
*(sizeof(int)*)param = 12;
预先感谢 !
我想您是在问如何访问结构中的特定字节。将指针类型转换为数组,然后将其作为数组访问。例如:
unsigned char *byteArray = (unsigned char *)scene;
byteArray[4] = 12; //sets the fifth byte (offsets start at 0) to 12.
我建议您使用位掩码而不是使用潜在的 UB。例如,如果t_scene是 32 位类型:
uint8_t lower_byte = scene & 0xff;
...
uint8_t high_byte = (scene >> 24) & 0xff;