本文介绍了这便携式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我可以按照以下方式在 中找到结构成员的偏移量: #define offsetof(type,member)(char *)(&((struct type *)0) - > member) - (char *)0 上面的宏中是否取消引用NULL指针? 解决方案 http://c-faq.com/ struct / offsetof.html 在实践中,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)) 第三,编译器拒绝接受哪种实现 ? 如何使用stddef。 h $> 并在那里使用偏移量? 问候 弗里德里希 - 请通过电子邮件删除just-for-news-回复。 Hi,Can I portably find the offset of a member of a structure in thefollowing way:#define offsetof(type,member) (char *)(&((struct type*)0)->member) -(char *)0Is the NULL pointer dereferenced in the above macro ? 解决方案 http://c-faq.com/struct/offsetof.htmlThe NULL pointer is typically not dereferenced in practice, but sinceit is in theory, it''s not valid. http://c-faq.com/struct/offsetof.htmlThe NULL pointer is typically not dereferenced in practice, but sinceit is in theory, it''s not valid.Thanks for the pointer. Still, I am not able to understand that , ifNULL 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?How about using stddef.hand use th offsetof in there?RegardsFriedrich--Please remove just-for-news- to reply via e-mail. 这篇关于这便携式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 14:53