C语言实例:结构体
2024-08-26 02:28:20
结构体:
#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()
最新文章
- Ucenter,Discuz
- (实用篇)PHP JSON数组与对象的理解
- 快速入门linux系统的iptables防火墙 1 本机与外界的基本通信管理
- 又来折腾Linux
- PCAP 抓包
- 用CSS创建小三角形问题(笔试题常考)
- AWT与Swing的区别
- Could not get dialect instance.
- JVM内存结构简单认知
- 移除文件(git rm)
- pyspider框架学习
- flag读取控制台参数
- BZOJ3944 Sum 数论 杜教筛
- Ubuntu 安装 chrome浏览器
- cnetos6上实现nfs共享
- MQTT_DEMO
- PLSQL计算质数
- Bootstrap入门八:图片
- DIV焦点事件详解 --【focus和tabIndex】​
- struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
热门文章
- java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec
- [转]BT原理分析
- NTSC PAL 介绍
- nginx break-circus
- C++/C代码审查注意事项(摘录,非原创)
- Win10安装和配置JDK
- spark核心优化详解
- redis 3.2 新数据结构:quicklist、String的embstr与raw编码方式分界点
- 爬虫 - 动态分页抓取 游民星空 的资讯 - bs4
- 2019CCF-GAIR全球人工智能与机器人峰会于7月在深圳召开