如何在结构上使用 offsetof() ?

发布于 2024-12-01 06:14:20 字数 529 浏览 1 评论 0原文

我想要 mystruct1 中的参数行 offsetof()。我已经尝试过

offsetof(struct mystruct1, rec.structPtr1.u_line.line) 

offsetof(struct mystruct1, line)  

但都不起作用。

union {
    struct mystruct1 structPtr1;
    struct mystruct2 structPtr2;
} rec;

typedef struct mystruct1 {
    union {
        struct {
            short len;
            char buf[2];
        } line;

        struct {
            short len;
        } logo;

    } u_line;
};

I want the offsetof() the param line in mystruct1. I've tried

offsetof(struct mystruct1, rec.structPtr1.u_line.line) 

and also

offsetof(struct mystruct1, line)  

but neither works.

union {
    struct mystruct1 structPtr1;
    struct mystruct2 structPtr2;
} rec;

typedef struct mystruct1 {
    union {
        struct {
            short len;
            char buf[2];
        } line;

        struct {
            short len;
        } logo;

    } u_line;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木落 2024-12-08 06:14:20

offsetof() 宏有两个参数。 C99 标准规定(在 §7.17 中):

offsetof(类型,成员指示符)

它扩展为一个具有size_t类型的整数常量表达式,其值是
这是结构成员(由成员指示符指定)的字节偏移量,
从其结构的开始(由类型指定)。类型和成员指示符
应该是给定的

静态类型 t;

然后表达式&(t.member-designator) 计算结果为地址常量。

因此,您需要这样写:

offsetof(struct mystruct1, u_line.line);

但是,我们可以观察到答案将为零,因为 mystruct1 包含一个 union 作为第一个成员(也是唯一的成员),并且 它的 >line 部分是联合的一个元素,因此它将位于偏移量 0 处。

The offsetof() macro takes two arguments. The C99 standard says (in §7.17 <stddef.h>):

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the value of
which is the offset in bytes, to the structure member (designated by member-designator),
from the beginning of its structure (designated by type). The type and member designator
shall be such that given

static type t;

then the expression &(t.member-designator) evaluates to an address constant.

So, you need to write:

offsetof(struct mystruct1, u_line.line);

However, we can observe that the answer will be zero since mystruct1 contains a union as the first member (and only), and the line part of it is one element of the union, so it will be at offset 0.

酒浓于脸红 2024-12-08 06:14:20

您的 struct mystruct1 有 1 个名为 u_line 的成员。 您可以看到该成员或该成员的偏移量

offsetof(struct mystruct1, u_line); // should be 0

如果您指定每个“亲子关系级别”,

offsetof(struct mystruct1, u_line.line);
offsetof(struct mystruct1, u_line.line.buf);
offsetof(struct mystruct1, u_line.logo);

Your struct mystruct1 has 1 member named u_line. You can see the offset of that member

offsetof(struct mystruct1, u_line); // should be 0

or of members down the line if you specify each "level of parenthood"

offsetof(struct mystruct1, u_line.line);
offsetof(struct mystruct1, u_line.line.buf);
offsetof(struct mystruct1, u_line.logo);
深巷少女 2024-12-08 06:14:20

首先,据我所知, offsetof 旨在仅与结构的直接成员一起使用(我的说法正确吗?)。

其次,了解流行 offsetof 实现的内部细节,我可以建议尝试

offsetof(struct mystruct1, u_line.line) 

这应该可行。它是否符合标准对我来说是一个悬而未决的问题。

PS 从@Jonathan Leffler 的回答来看,这实际上应该有效。

Firstly, AFAIK, offsetof is intended to be used with immediate members of the struct only (am I right on this?).

Secondly, knowing the internal details of popular offsetof implementations I can suggest trying

offsetof(struct mystruct1, u_line.line) 

This should work. Whether it is standard-compliant is an open question for me.

P.S. Judging by @Jonathan Leffler's answer, this should actually work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文