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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何从从官网下载各个版本的jquery「建议收藏」

    如何从从官网下载各个版本的jquery「建议收藏」许多前端的小伙伴们可能跟我一样有选择强迫症,对于一些工具、软件等都偏爱从官网下载,尽管许多非官方网站上已经有现成的,但还是从心理上感觉官网的更正规。如今的jquery版本已经是相当多了。在jquery官网的首页上只提供了为数不多的较为流行的版本供我们下载。但是出于各种情况的考虑,我们想要自己需要的某一版本该如何从官网获取呢?步骤也是相当简单,jquery官网虽然为了页面的简洁性并未在Downl…

    2022年5月25日
    38
  • 苹果CMS

    苹果CMS

    2021年10月31日
    71
  • WEB-INF目录与META-INF目录的作用

    WEB-INF目录与META-INF目录的作用

    2022年3月1日
    41
  • java_简易画图板

    java_简易画图板下面我将分享用Java制作简易画图板的过程。version1Draw.javaJava代码importjavax.swing.JFrame;/****@authoryangzhenlin*

    2022年7月2日
    22
  • 静态库和动态库区别

    静态库和动态库区别转自静态库和动态库区别前言我们在编写代码的时候经常用到已有的接口 他们是以库的形式提供给我们使用的 而常见形式有两种 一种常以 a 为后缀 为静态库 另一种以 so 为后缀 为动态库 那么这两种库有什么区别呢 说明 本文主要说明 Linux 下的情况 windows 不涉及 目标文件在解释静态库和动态库之前 需要简单了解一下什么是目标文件 目标文件常常按照特定格式来组织 在 linux 下 它是 ELF 格式 ExecutableLi 可执行可链接格式 而在 windows 下是 PE P

    2025年7月6日
    7
  • oracle rac数据库srvctl命令格式参考[通俗易懂]

    oracle rac数据库srvctl命令格式参考[通俗易懂]用法:srvctl[-V]用法:srvctladddatabase-d<db_unique_name>-o<oracle_home>[-c{RACONENODE|RAC|SINGLE}[-e<server_list>][-i<inst_name>][-w<timeout>]][-m<domain_name>][-p<spfile>][-r{PRIMARY|PHYSI

    2025年8月30日
    5

发表回复

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

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