offsetof_firetaker的专栏

电脑杂谈  发布时间:2016-05-30 02:00:21  来源:网络整理

你是否正在寻找关于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的全部内容,相信你一定会非常满意。


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shenmilingyu/article-6828-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 古之奇
        古之奇

        我就静静的的看着你们抢

      • 黄炳琦
        黄炳琦

        自称青龙行于海

      • 王晓萍
        王晓萍

        那要看我们国家的变革力度够不够大持不持久

      热点图片
      拼命载入中...