第10课 struct 和 union 分析
1. struct的小秘密
(1)C语言中的struct可以看作变量的集合
(2)struct的问题——空结构体占用多的内存?
【实例分析】空结构体的大小
#include <stdio.h> struct TS
{ }; int main()
{
struct TS t1;
struct TS t2; //VC、BCC下定义空结构体编译器直接报错,gcc下空结构体大小为0
printf("sizeof(struct TS) = %d\n", sizeof(struct TS));
printf("sizeof(t1) = %d, &t1 = %p\n", sizeof(t1), &t1);
printf("sizeof(t2) = %d, &t2 = %p\n", sizeof(t2), &t2); return ;
}
2. 结构体与柔性数组
(1)柔性数组即数组大小待定的数组
(2)C语言中可以由结构体产生柔性数组
(3)C语言中结构体的最后一个元素可以是大小未知的数组
struct SoftArray
{
int len;
int array[]; //array仅是一个待使用的标识符。与指针不同,编译器
//并不为array变量分配空间,因为也不知道array究竟
//多大。只是用来作为一个标识符,以便以后可以通过这
//个标识符来访问其中的内容。所以sizeof(SoftArray)=4
}
(4)柔性数组的用法

struct SoftArray* sa = NULL;
//注意,因sizeof柔性数组并不包含array大小,所以要开辟的空间总大小应等于
//柔性数组+数组各元素所占的空间,即空间大小等于结构体的大小(len域)加上数组的大小
sa = (struct SoftArray*)malloc(sizeof(struct SoftArray)+sizoef(int)*);
sa->len = ;
【实例分析】柔性数组使用分析
#include <stdio.h>
#include <malloc.h> struct SoftArray
{
int len;
int array[];
}; struct SoftArray* create_soft_array(int size)
{
struct SoftArray* ret = NULL; if( size > )
{
ret = (struct SoftArray*)malloc(sizeof(struct SoftArray) + sizeof(int) * size); ret->len = size;
} return ret;
} void delete_soft_array(struct SoftArray* sa)
{
free(sa);
} void func(struct SoftArray* sa)
{
int i = ; if( NULL != sa )
{
for(i=; i<sa->len; i++)
{
sa->array[i] = i + ;
}
}
} int main()
{
int i = ;
struct SoftArray* sa = create_soft_array(); func(sa); for(i=; i<sa->len; i++)
{
printf("%d\n", sa->array[i]);
} delete_soft_array(sa); return ;
}
3. C语言中的union
(1)C语言中的union在语法上与struct相似
(2)union只分配最大成员的空间,所有成员共享这个空间

(3)union的使用受系统大小端的影响

【编程实验】判断系统的大小端
#include <stdio.h> int system_mode()
{
union SM
{
int i;
char c;
}; union SM sm; sm.i = ; return sm.c;
} int main()
{
//返回1时为小端,0为大端模式
printf("System Mode: %d\n", system_mode());
return ;
}
4. 小结
(1)struct中的每人数据成员有独立的存储空间
(2)struct可以通过最后的数组标识符产生柔性数组
(3)union中的所有数据成员共享同一个存储空间
(4)union的使用会受到系统大小端的影响
第10课 struct 和 union 分析的更多相关文章
- 第10课 struct和union分析
struct的小秘密:空结构体占多大内存呢? 直观的答案有两种: 1.空结构体的大小为0 2.结构体本来就是为了将不同的变量集合在一起使用的,定义空结构体会导致编译错误 实例分析: #include ...
- C语言进阶——struct和union分析10
struct的小秘密: C语言中的struct可以看作变量的集合 struct的问题:空结构体占用多大内存呢? 程序实例1: #include <stdio.h> struct TS { ...
- struct和union分析实例
1.#include <stdio.h>#include <malloc.h>typedef struct _soft_array{ int len; int ar ...
- 关于C中struct和union长度的详解
这几天看<代码大全>中的第十三章---不常见的数据类型,里面讲解到了C语言中的struct以及对指针的解释,联想到以前看过相关的关于C语言中stuct长度的文章,只是现在有些淡忘了,因此今 ...
- struct和union
struct的小秘密 C语言中的struct可以看做变量的集合,struct的问题: 空结构体占用多大内存? 例子1:空结构体的大小 #include<stdio.h> struct ST ...
- 第11课 - enum, sizeof, typedef 分析
第11课 - enum, sizeof, typedef 分析 1. enum介绍 (1)enum是C语言中的一种自定义类型,和struct.union地位相同,格式如下: // enum每个值的最后 ...
- 关于 typedef & typedef struct & typedef union理解 --写给不长脑子的我
来源: http://zhidao.baidu.com/link?url=qxzkx5gaoCfnHnygYdzaLEWkC45JqNYYUk42eHHjB0yB3ZMgHv6lGjnq3CRfgQw ...
- 【集美大学1411_助教博客】团队作业10——项目复审与事后分析(Beta版本)
写在前面的话 软件工程课结束了,大家开心吗?是不是再也不用熬夜写代码了?如果这门课你真的熬夜写代码了,相信你一定有收获,如果这门课结束了你觉得是自己一个全新的开始,那么这门课的意义就实现了.团队作业全 ...
- <转> Struct 和 Union区别 以及 对内存对齐方式的说明
转载地址:http://blog.csdn.net/firefly_2002/article/details/7954458 一.Struct 和 Union有下列区别: 1.在存储多个成员信息时,编 ...
随机推荐
- Ubuntu 12.04硬盘安装教程
从服务器下载Ubuntu 12.04光盘镜像文件到 C 盘.下载地址:\\192.167.100.225\share\Tool\Ubuntu\ubuntu-12.04.1-desktop-amd64. ...
- TCP状态转化图 TIME_WAIT解析
先上转换图: 重点研究TIME_WAIT状态,根据UNIX网络编程中的思路,TIME_WAIT状态有两个存在的理由: 理由1. 客户端执行主动关闭,假设最终的ACK丢失,服务器将重新发送它的最后那个F ...
- 学习magento要学哪些知识
php框架水平,具体点的就是大名鼎鼎的ZF框架.别急,先还是熟悉下OSC吧,主要是热身下商城的那些业务流的知识,基本的数据流程.自己做模板的话CSS2.0水平还不能太低.JS框架JQ吧相对简单点.当然 ...
- jqprint 打印分页
再要分页的结尾处加上<div style="page-break-after:always;"></div> 就可以了
- scroll滚动动画(js/ts)
//(蓝色this部分为dom) scrollToLeft(option?: { duration?: number, direction?: number }) { let direction = ...
- js三级联动
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- hdu1166 敌兵布阵 树状数组/线段树
数列的单点修改.区间求和 树状数组或线段树入门题 #include<stdio.h> #include<string.h> ],N; void add(int x,int a) ...
- apache spark kubernets 部署试用
spark 是一个不错的平台,支持rdd 分析stream 机器学习... 以下为使用kubernetes 部署的说明,以及注意的地方 具体的容器镜像使用别人已经构建好的 deploy yaml 文件 ...
- 使用C#和MSMQ开发消息处理程序
简介 MSMQ(微软消息队列)是Windows操作系统中消息应用程序的基础,是用于创建分布式.松散连接的消息通讯应用程序的开发工具.消息队列和电子邮件有着很多相似处,他们都包含多个属性,用于保存消息, ...
- 横竖两个数字塔的效果BAT批处理怎么写?
横竖两个数字塔的效果BAT批处理怎么写?@echo offfor /l %%a in (0,1,1) do ( for /l %%i in (0,1,9) do ( for ...