offsetof 例子「建议收藏」

offsetof 例子「建议收藏」
/*offsetofexample*/
#include”stdafx.h”
#include
#include
/************************************************************************/
/*MacroOBJECT_HEAD_ADDRESS:calculatethestruct’saddress

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

/* 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.net/172108.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C语言之strstr函数

    C语言之strstr函数【FROMMSDN&&百科】原型:char*strstr(constchar*str1,constchar*str2);#include找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。Returnsapointertothefirstoccurrence

    2022年6月25日
    27
  • 宝塔linux面板.txt

    宝塔linux面板.txt

    2021年9月23日
    54
  • C# 特性(Attribute)之Flag特性

    C# 特性(Attribute)之Flag特性

    2022年3月2日
    41
  • 前端vscode必备插件推荐(墙裂推荐)「建议收藏」

    前端vscode必备插件推荐(墙裂推荐)「建议收藏」前言:vscode是一款强大的前端编辑软件,有些人说ws(webstorm)更好用,但是vs重在轻量级啊!!!而且根据自己的开发习惯安装适合自己的插件后,用起来简直不要太舒服了好嘛!!!首先呢,我先推荐的就是最基础的语言包,没办法,英语水平太捞了哈哈哈,弄起来后就舒服多了,汉语yyds~《Chinese(Simplified)(简体中文)Language》注释工具《ColorfulComments》不同的注释符能带来很多高亮的显示快速找到css定义位置并小窗口展示

    2022年7月25日
    13
  • android toast防重_如何解决android Toast重复显示

    android toast防重_如何解决android Toast重复显示Toast是一种简易的消息提示框,它无法获取焦点,按设置的时间来显示完以后会自动消失,一般用于帮助或提示。先给大家分享下我的解决思路:不用计算Toast的时间之类的,就是定义一个全局的成员变量Toast,这个Toast不为null的时候才去make,否则直接setText.为了按返回键后立即使Toast不再显示,重写父类Activity的onBackPressed()方法里面去cancel你的T…

    2022年9月13日
    5
  • 微信小程序 弹出框_如何设置小程序弹窗公告

    微信小程序 弹出框_如何设置小程序弹窗公告消息提示框 模态弹窗 操作菜单1.消息提示——wx.showToast(OBJECT)//show.js//获取应用实例varapp=getApp()Page({ showok:function(){ wx.showToast({ title:’成功’, icon:’success’, duration:

    2025年7月3日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号