你是否正在寻找关于offsetof的内容?让我把最吸引人的东西奉献给你:
offsetof :
Retrieves the offset of a member from the beginning of its parent structure.
size_t offsetof(structName, memberName);
Parameters:
structName : Name of the parent data structure.
memberName :Name of the member in the parent data structure for which to determine the offset.
Return Value : offsetof returns the offset in bytes of the specified member from
the beginning of its parent data structure. It is undefined for bit fields.
Remarks :
The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.
Note :
offsetof is not a function and cannot be described using a C prototype.
#define offsetof(s, m) (size_t)&(((s *)0)->m)
s是一个结构名,它有一个名为m的成员(s和m 是宏offsetof的形参,它实际是返回结构s的成员m的偏移地址.
(s *)0 是骗编译器说有一个指向类(或结构)s的指针,其地址0
&((s *)0)->m 是要取得类s中成员变量m的地址. 因基址为0,这时m的地址当然就是m在s中的偏移
最后转换size_t 型,即unsigned int,。
以上就是关于offsetof的全部内容,相信你一定会非常满意。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shenmilingyu/article-6828-1.html
找个退役船撞它