打印结构构件的偏移量 [英] Printing offset of a member of structure

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

问题描述

我正在尝试打印一个特定成员在

结构中的偏移量,但它不起作用。我使用以下

表达式来打印偏移量。


&(struct my_struct *)0-> member_name

这有什么问题?


如果我使用,

&((struct my_struct *)0) - > member_name,它的工作原理。但似乎错误的是b $ b。看起来好像我正在尝试打印

结构本身的地址而不是会员。

提前填写任何帮助....

I am trying to print the offset of a particulat member in
a structure, but it''s not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What''s wrong with this ?

If I use,
&((struct my_struct *)0)->member_name, it works. But it seems
to be wrong. It looks as if I am trying to print the address of
structure itself instead of the member.
Thanx in advance for any help ....

推荐答案

ju *** *******@yahoo.co.in 写道:
我试图在一个结构中打印一个特定成员的偏移,但它''不工作


使用< stddef.h>中定义的offsetof()宏。这就是它的价值。

我使用以下
表达式打印偏移量。

&(struct my_struct *)0-> member_name

这有什么问题?


- >运算符绑定比投射更紧密,所以你试图
投射0-> member_name的结果。

如果我使用,
& ;((struct my_struct *)0) - > member_name,它的工作原理。但似乎错了。看起来好像我正在尝试打印
结构本身的地址而不是成员。
I am trying to print the offset of a particulat member in
a structure, but it''s not working.
Use the offsetof() macro, defined in <stddef.h>. That''s what it''s
for.
I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What''s wrong with this ?
The -> operator binds more tightly than a cast, so you''re trying to
cast the result of 0->member_name.
If I use,
&((struct my_struct *)0)->member_name, it works. But it seems
to be wrong. It looks as if I am trying to print the address of
structure itself instead of the member.




offsetof()的一个可能定义是:


#define offsetof(s,m)(size_t)(&(((s *)0) - > m))


(s *)0是指向地址为零的结构的指针。


((s *)0) - > m是该结构的成员。


如果成员位于结构中的偏移6处,那么

的成员地址0的结构将具有6的地址。


将该地址转换为size_t会得到值6,这就是你想要的



*所有这些都是特定于系统的。这取决于关于如何表示地址,从

地址到size_t的转换等众多

假设。你不能在

可移植代码中做出假设 - 但是运行时库的实现者有权做出编译器支持的任何假设。


这就是为什么offsetof()宏在标准库中定义的原因;

没有可移植的方法来实现它,但总有一个

适用于给定实现的非可移植方式。实施者

被允许做你不是的事情。


-

Keith Thompson(The_Other_Keith)< a href =mailto:ks *** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



One possible definition of offsetof() is:

#define offsetof(s, m) (size_t)(&(((s *)0)->m))

(s *)0 is a pointer to a struct at address zero.

((s *)0)->m is a member of that structure.

If the member is at offset 6 within the structure, that member of
the structure at address 0 will have an address of 6.

Converting that address to size_t yields the value 6, which is what
you want.

*All* of this is extremely system-specific. It depends on numerous
assumptions about how address are represented, conversions from
addresses to size_t, and so forth. You cannot make assumptions in
portable code -- but the implementer of the runtime library is
entitled to make whatever assumptions are supported by the compiler.

That''s why the offsetof() macro is defined in the standard library;
there''s no portable way to implement it, but there''s always a
non-portable way that works for a given implementation. The implementer
is allowed to do things that you aren''t.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


ju * *********@yahoo.co.in 写道:
ju**********@yahoo.co.in wrote:
我正在尝试打印特定成员的偏移量
a结构,但它不起作用。我使用以下
表达式打印偏移量。

&(struct my_struct *)0-> member_name

这有什么问题?
I am trying to print the offset of a particulat member in
a structure, but it''s not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What''s wrong with this ?




它没有使用标准为此目的提供的宏,

那是'什么'错了用它。此外,您没有阅读常见问题解答。

再次。


Richard



It doesn''t use the macro the Standard provides for this very purpose,
that''s what''s wrong with it. Also, you have failed to read the FAQ.
Again.

Richard


ju ********** @ yahoo.co.in 写道:

我试图在一个结构中打印一个特定成员的偏移量,但它不起作用。我使用以下
表达式打印偏移量。

&(struct my_struct *)0-> member_name

这有什么问题?

I am trying to print the offset of a particulat member in
a structure, but it''s not working. I am using the following
expression to print the offset.

&(struct my_struct *)0->member_name

What''s wrong with this ?




它与现实无关。然而,有一个标准的

宏,名为offsetof,以满足您的需求。


-

一些信息链接:

新闻:news.announce.newusers
http:/ /www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html



It has nothing to do with reality. However there is a standard
macro, named offsetof, to satisfy your urge.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html


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

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