结构内部成员的偏移量 [英] offset of a member inside a structure

查看:94
本文介绍了结构内部成员的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到成员mbr的偏移量。在一个结构内

" str"如下:


offset =&(struct str *)0-> mbr;


但是,在编译时我得到以下内容错误:

cc:错误:m1.c,第55行:在此声明中,0有一个签名的int

类型,但发生在需要指针的上下文中。 (needpointer)


有人可以指出问题出在哪里吗?


Thanx提前提供任何帮助....

I am trying to find the offset of a member "mbr" inside a structure
"str" as follows:

offset = &(struct str *)0->mbr;

But, on compilation I get the following error:
cc: Error: m1.c, line 55: In this statement, "0" has a signed int
type, but occurs in a context that requires a pointer. (needpointer)

Can anybody point out where the problem is ?

Thanx for any help in advance....

推荐答案

junky_fellow写道:
junky_fellow wrote:
我试图找到一个成员的偏移量mbr在结构内部
str如下:

offset =&(struct str *)0-> mbr;

但是,在编译时我收到以下错误:
cc:错误:m1.c,第55行:在此声明中,0表示0。有一个签名的int
类型,但发生在需要指针的上下文中。 (needpointer)

有人能指出问题所在吗?
I am trying to find the offset of a member "mbr" inside a structure
"str" as follows:

offset = &(struct str *)0->mbr;

But, on compilation I get the following error:
cc: Error: m1.c, line 55: In this statement, "0" has a signed int
type, but occurs in a context that requires a pointer. (needpointer)

Can anybody point out where the problem is ?




是的。你没有使用< stddef.h>中的offsetof宏。


[表达式`(type)foo-> bar`解析为`(type)(foo) - >巴)`。你需要`&b $ b&b;(结构str *)0) - > mbr`你想要做什么。

然而,你在尝试什么做(a)有不确定的行为和

(b)并没有返回我(至少)认为是偏移的东西。

成员,在合理的实施中。什么,确切地说,*你是* b $ b想要做什么?]

-

Chris" electric hedgehog" Dollin



Yes. You''re not using the offsetof macro from <stddef.h>.

[The expression `(type) foo->bar` parses as `(type) (foo->bar)`. You
needed `&((struct str *)0)->mbr` for what you were trying to do.
However, what you''re trying to do (a) has undefined behaviour and
(b) doesn''t return what I (at least) think of as the "offset" of
a member, in plausible implementations. What, exactly, *are* you
trying to do?]
--
Chris "electric hedgehog" Dollin


junky_fellow写道:
junky_fellow wrote:

我试图找到一个成员的偏移量mbr在结构内部
str如下:

offset =&(struct str *)0-> mbr;

但是,在编译时我收到以下错误:
cc:错误:m1.c,第55行:在此声明中,0表示0。有一个签名的int
类型,但发生在需要指针的上下文中。 (needpointer)

任何人都可以指出问题所在吗?

I am trying to find the offset of a member "mbr" inside a structure
"str" as follows:

offset = &(struct str *)0->mbr;

But, on compilation I get the following error:
cc: Error: m1.c, line 55: In this statement, "0" has a signed int
type, but occurs in a context that requires a pointer. (needpointer)

Can anybody point out where the problem is ?




我相信 - >优先级高于演员。 (虽然如果

我错了,很快就会有人纠正这个问题。)


试试:


offset =&(((struct str *)0) - > mbr);


我的C编译器有:


#define offsetof(__ typ,__ id)((size_t)&(((__ typ *)0) - > __ id))


-

+ ------------------------- + -------------------- + - ---------------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +



I believe that "->" has higher precedence than the cast. (Though if
I''m wrong, someone will be along shortly to correct that.)

Try:

offset = &(((struct str *)0)->mbr);

My C compiler has:

#define offsetof(__typ,__id) ((size_t)&(((__typ*)0)->__id))

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+


你好Chris,

Chris Dollin写道:
Hi Chris,
Chris Dollin wrote:
junky_fellow写道:

junky_fellow wrote:

我是试图找到成员mbr的偏移量。在结构内部
str如下:

offset =&(struct str *)0-> mbr;

但是,在编译时我收到以下错误:
cc:错误:m1.c,第55行:在此声明中,0表示0。有一个签名的int
类型,但发生在需要指针的上下文中。 (needpointer)

有人能指出问题所在吗?
I am trying to find the offset of a member "mbr" inside a structure
"str" as follows:

offset = &(struct str *)0->mbr;

But, on compilation I get the following error:
cc: Error: m1.c, line 55: In this statement, "0" has a signed int
type, but occurs in a context that requires a pointer. (needpointer)

Can anybody point out where the problem is ?



是的。你没有使用< stddef.h>中的offsetof宏。

[表达式`(type)foo-> bar`解析为`(type)(foo-> bar )`。你需要`&((struct str *)0) - > mbr`用于你想要做的事情。
然而,你想要做的事情(a)有未定义的行为并且(b)没有返回我(至少)认为是偏移的东西。
成员,在合理的实施中。什么,确切地说,*是*你想要做什么?]


Yes. You''re not using the offsetof macro from <stddef.h>.

[The expression `(type) foo->bar` parses as `(type) (foo->bar)`. You
needed `&((struct str *)0)->mbr` for what you were trying to do.
However, what you''re trying to do (a) has undefined behaviour and
(b) doesn''t return what I (at least) think of as the "offset" of
a member, in plausible implementations. What, exactly, *are* you
trying to do?]




好​​吧,除了标准标题当然可以这样的事实
只要他们以标准要求的方式提供标准要求,
按照实现定义的方式执行他们想要的操作:

在大多数实现中,我知道你会发现offsetof定义为



#define offsetof(TYPE,MEMBER)((size_t)&((TYPE *)0) - >会员)

差异主要在size_t类型中。


所以,也许OP已经在某个地方见过这个并想重现它。


顺便说一句:这真的会调用UB吗?

干杯

Michael



Well, apart from the fact that the standard headers can of course
do what they want in an implementation defined way as long as they
provide what the standard demands in the way the standard demands:
In most implementations I know you find offsetof to be defined along
the lines of
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
with differences mainly in the size_t typecast.

So, maybe the OP has seen this somewhere and wanted to reproduce it.

BTW: Does this really invoke UB?
Cheers
Michael


这篇关于结构内部成员的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆