这便携式吗? [英] is this portable ?

查看:72
本文介绍了这便携式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我可以按照以下方式在

中找到结构成员的偏移量:


#define offsetof(type,member)(char *)(&((struct type *)0) - > member) -

(char *)0


上面的宏中是否取消引用NULL指针?

解决方案

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





我可以按照以下方式在

中找到结构成员的偏移量:


#define offsetof(type,member)(char *)(&((struct type *)0) - > member) -

(char *) 0


上面的宏中是否取消引用了NULL指针?

http://c-faq.com/ struct / offsetof.html


在实践中,NULL指针通常不会被解引用,但是因为它理论上是

,所以它是无效。




Harald van D?3k写道:

< a href =mailto:ju ********** @ yahoo.co.in> ju ********** @ yahoo.co.in 写道:





我可以在

中可移植地找到结构成员的偏移量以下方式:


#define offsetof(type,member)(char *)(&((struct type *)0) - > member) -

(char *)0

上面的宏中是否取消引用了NULL指针?


http:// c- faq.com/struct/offsetof.html


在实践中,NULL指针通常不会被解引用,但是因为理论上它是
它无效。



感谢指针。不过,我无法理解,如果

空指针没有被解除引用那么为什么它无效?


其次,在宏中(来自FAQ列表)

#define offsetof(type,f)((size_t)\

((char *)&((type *)0) - > ; f - (char *)(类型*)0))

^^^

为什么我们首先输入强制转换NULL指针(类型*)然后到(char *)

?我们不能简单地做

#define offsetof(type,f)((size_t)\

((char *)&((type *)0 ) - > f - (char *)0))

第三,编译器拒绝接受哪种实现




"菊********** @ yahoo.co.in" < ju ********** @ yahoo.co.inwrites:





我可以按照下面的方式在

中找到结构成员的偏移量:

#define offsetof(type,member)(char *)( &(;(struct type *)0) - > member) -

(char *)0



如何使用stddef。 h $>
并在那里使用偏移量?


问候

弗里德里希


-

请通过电子邮件删除just-for-news-回复。


Hi,

Can I portably find the offset of a member of a structure in the
following way:

#define offsetof(type,member) (char *)(&((struct type*)0)->member) -
(char *)0

Is the NULL pointer dereferenced in the above macro ?

解决方案

ju**********@yahoo.co.in wrote:

Hi,

Can I portably find the offset of a member of a structure in the
following way:

#define offsetof(type,member) (char *)(&((struct type*)0)->member) -
(char *)0

Is the NULL pointer dereferenced in the above macro ?

http://c-faq.com/struct/offsetof.html

The NULL pointer is typically not dereferenced in practice, but since
it is in theory, it''s not valid.



Harald van D?3k wrote:

ju**********@yahoo.co.in wrote:

Hi,

Can I portably find the offset of a member of a structure in the
following way:

#define offsetof(type,member) (char *)(&((struct type*)0)->member) -
(char *)0

Is the NULL pointer dereferenced in the above macro ?


http://c-faq.com/struct/offsetof.html

The NULL pointer is typically not dereferenced in practice, but since
it is in theory, it''s not valid.

Thanks for the pointer. Still, I am not able to understand that , if
NULL pointer is not dereferenced then why it is not valid ?

Secondly, in the macro ( from the FAQ list)
#define offsetof(type, f) ((size_t) \
((char *)&((type *)0)->f - (char *)(type *)0))

^^^
why do we first type cast NULL pointer to (type *) and then to (char *)
? Can''t we simply do
#define offsetof(type, f) ((size_t) \
((char *)&((type *)0)->f - (char *)0))
Thirdly, on which implementation the compiler would refuse to accept it
?


"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:

Hi,

Can I portably find the offset of a member of a structure in the
following way:

#define offsetof(type,member) (char *)(&((struct type*)0)->member) -
(char *)0

How about using stddef.h
and use th offsetof in there?

Regards
Friedrich

--
Please remove just-for-news- to reply via e-mail.


这篇关于这便携式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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