大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/172108.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...