前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >offsetof 例子「建议收藏」

offsetof 例子「建议收藏」

作者头像
全栈程序员站长
发布2022-09-27 21:34:59
2070
发布2022-09-27 21:34:59
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

/* offsetof example */

#include “stdafx.h” #include <stdio.h> #include <stddef.h>

/************************************************************************/ /* Macro OBJECT_HEAD_ADDRESS : calculate the struct’s address according /* to one of its member’s address /************************************************************************/ #define OBJECT_HEAD_ADDRESS(ClassName,MemberName,Addre) / Addre – offsetof(ClassName, MemberName)

struct S { int ID; char *addr; char name[20]; };

struct S1 { S employee; char *title; };

struct S2 { char singlechar; int arraymember[10]; char anotherchar; };

int main(int argc, char* argv[]) { /* Example 1 S2 s2 = {‘a’, 10, ‘b’}; int head = int(&(s2.singlechar)); int memb = int(&(s2.anotherchar));

int i = OBJECT_HEAD_ADDRESS(S2, anotherchar, memb);

printf (“offsetof(S2, singlechar) is %d/n”, offsetof(S2, singlechar)); printf (“offsetof(S2, arraymember) is %d/n”, offsetof(S2, arraymember)); printf (“offsetof(S2, anotherchar) is %d/n”, offsetof(S2, anotherchar)); printf(“s2.anotherchar’s address is %x ==> s2’s address is %x/n”, memb, i); */ /* Example 2 S s = {100, “Nanjing”, “Thomas”}; int id = int(&(s.ID)); int ad = int(&(s.addr)); int nm = int(&(s.name));

int j = OBJECT_HEAD_ADDRESS(S, addr, ad);

printf (“offsetof(S, ID) is %d/n”, offsetof(S, ID)); printf (“offsetof(S, addr) is %d/n”, offsetof(S, addr)); printf (“offsetof(S, name) is %d/n”, offsetof(S, name)); printf (“s.ID’s address : %x/ns.addr’s address : %x/ns.name’s address : %x/n”, id, ad, nm); printf(“s.addr’s address is %x ==> s’s address is %x/n”, ad, j); */ /* Example 3 */ S1 s1 = { {100, “Nanjing”, “Thomas Chen”},”Thomas Chen”}; int td = int(&(s1.title)); int k = OBJECT_HEAD_ADDRESS(S1, title, td);

printf (“offsetof(S1, employee) is %d/n”, offsetof(S1, employee)); printf (“offsetof(S1, title) is %d/n”, offsetof(S1, title)); printf(“s1.title’s address is %x ==> s1’s address is %x/n”, td, k);

return 0; } /* Example 1 Output */ offsetof(S2, singlechar) is 0 offsetof(S2, arraymember) is 4 offsetof(S2, anotherchar) is 44 s2.anotherchar’s address is 12ff7c ==> s2’s address is 12ff50

/* Example 2 Output */ offsetof(S, ID) is 0 offsetof(S, addr) is 4 offsetof(S, name) is 8 s.ID’s address : 12ff64 s.addr’s address : 12ff68 s.name’s address : 12ff6c s.addr’s address is 12ff68 ==> s’s address is 12ff64

/* Example 3 Output */ offsetof(S1, employee) is 0 offsetof(S1, title) is 28 s1.title’s address is 12ff7c ==> s1’s address is 12ff60

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/172108.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年8月22日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档