结构体:

#include <stdio.h>
#include <stdlib.h>

//#pragma pack(1)
typedef struct{
    short i; // 2 bytes
    int j;   // 4 bytes
    int t;   // 4 bytes
} test;
//#pragma pack()

int main()
{
    printf("main start!\n");

    test *pt = NULL;
    printf("%x\n", &(pt->t) );

    printf()->t) );

    printf("main end!\n");
    ;
}

设置对齐方式:

#include <stdio.h>
#include <stdlib.h>

#pragma pack(1)
typedef struct{
    short i; // 2 bytes
    int j;   // 4 bytes
    int t;   // 4 bytes
} test;
#pragma pack()

int main()
{
    printf("main start!\n");

    test *pt = NULL;
    printf("%x\n", &(pt->t) );

    printf()->t) );

    printf("main end!\n");
    ;
}

上面的程序用来打印出结构体中元素 t 在结构体中的偏移地址,#pragma用来设置对齐方式。

应用场景:

根据元素的实际地址计算出结构体的基地址(元素的实际地址 - 元素在结构体中的偏移地址)

UDK中的宏_CR:#define  _CR(Record, TYPE, Field)   ( (TYPE*) ( (CHAR8*)(Record)-(CHAR8*)&(((TYPE*)0)->Field)  ) )

Linux中的Container_of()

最新文章

  1. Ucenter,Discuz
  2. (实用篇)PHP JSON数组与对象的理解
  3. 快速入门linux系统的iptables防火墙 1 本机与外界的基本通信管理
  4. 又来折腾Linux
  5. PCAP 抓包
  6. 用CSS创建小三角形问题(笔试题常考)
  7. AWT与Swing的区别
  8. Could not get dialect instance.
  9. JVM内存结构简单认知
  10. 移除文件(git rm)
  11. pyspider框架学习
  12. flag读取控制台参数
  13. BZOJ3944 Sum 数论 杜教筛
  14. Ubuntu 安装 chrome浏览器
  15. cnetos6上实现nfs共享
  16. MQTT_DEMO
  17. PLSQL计算质数
  18. Bootstrap入门八:图片
  19. DIV焦点事件详解 --【focus和tabIndex】​
  20. struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

热门文章

  1. java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec
  2. [转]BT原理分析
  3. NTSC PAL 介绍
  4. nginx break-circus
  5. C++/C代码审查注意事项(摘录,非原创)
  6. Win10安装和配置JDK
  7. spark核心优化详解
  8. redis 3.2 新数据结构:quicklist、String的embstr与raw编码方式分界点
  9. 爬虫 - 动态分页抓取 游民星空 的资讯 - bs4
  10. 2019CCF-GAIR全球人工智能与机器人峰会于7月在深圳召开