gpt4 book ai didi

c - 如何将结构重铸为 u32?

转载 作者:行者123 更新时间:2023-12-04 10:27:59 24 4
gpt4 key购买 nike

在我调用的 API 中,它有一个“extenededmode”字段,允许我传递我的用户特定扩展:

  750 struct my_struct {
751 __u32 field1;
752 __u32 field2;
753 __u32 field3;
754 __u32 extendedmode; /* user extension */
755 __u32 field4;
756 __u32 reserved[4];
757 };

所以我想使用该字段将我的结构放在那里。

struct my_ext {
_8 ext1;
_8 ext2;
_16 ext3;
}

那么如何将 my_ext(其大小为 u32)置于 my_struct 的扩展模式?

谢谢。

最佳答案

In the case of the following order of the fields:
extendedmode bit31-bit16 bit15-bit8 bit7-bit0
| | |
my_ext _16 ext3 _8 ext2 _8 ext1

我认为@pat 是对的,但它只能用于大端处理器。

对于小端处理器, union 可以这样写:

union my_ext {
_32 value;
struct my_ext {
_16 ext3;
_8 ext2;
_8 ext1;
};
}

关于c - 如何将结构重铸为 u32?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22550666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com