当前位置:文档之家› 哈工大C语言Struct

哈工大C语言Struct

的字节数
11/24/2019
Structures
6
struct类型的特点
一个普通的类型
– 所以可以定义该类型的变量、数组、指针…… – 它的成员可以是任意类型
– 可以基做本函类数型的、参E数数v组类a、l型指u针和at、返io结回n构值o…类…n型ly. eated wisthtrAucspt类os型e.的Sl变id量es for .NET 3.5 Client Profile 5.2.0
C程E序valu设atio计n on语ly. 言
eated with Aspose.Slides for .NET 3.5 Client Profile 5.2.0
Copyright 20第046-章201结1 A构spose Pty Ltd.
孙志岗 sun@
从基本数据类型、复合数据类型 到抽象数据类型
计算机本身是没有数据类型的概念的 – 内存里存储的内容,你认为它代表什么,它就代表什么
高级语言设计了基本数据类型:整型、浮点型、字符
型等。不同的语E言v也al会ua定ti义on不o同n的ly基. 本类型
– 基本数据类型并不能方便地解决所有问题
eated wi复th合A数sp据o类se型.S是li基de本s数fo据r 类.N型E迭T代3派.5生C而li来ent Profile 5.2.0 –C典o型p的yr代ig表h就t 是2“00结4构-2”0,1数1组A、s指po针s也e可P算ty作L此t类d.
–C常o用p类yr型ight 2004-2011 Aspose Pty Ltd.
基本数据类型、指针、数组、结构……
– 常见的需要类型的地方
定义变量 定义指针、数组和结构 函数参数和返回值
sizeof
11/24/2019
Structures
17
C语言中的三大定律
参数传递定律 – 函数调用时的参数传递永远都是传值调用,把实
{
int x;
int };
y;
Evaluation only.
eated wisthtrAuscptosep.oSilindtespfto;r .NET 3.5 Client Profile 5.2.0
stCroupcytrigphoti2n0t0*4-p2p0t1;1 Aspose Pty Ltd.
11/24/2019
Structures
3
C语言的解决办法
struct person { char name[12];
ucnhsairgsneexd;Eivnatluaagteio;n only. eated wi}th; Aspose.Slides for .NET 3.5 Client Profile 5.2.0
每个位段都可以当作一个无符号整型数使用
– 表达范围当然受限,而且当然不能取地址
11/24/2019
Structures
12
联合(Union)
union u_tag { int ival;
float fvEavla;luation only. eated with Acshpaors*e.Sslvidaels;for .NET 3.5 Client Profile 5.2.0
– rt.pt1.x
int y; };
Evaluatio–n (o*nrpl)y.. pt1.x
– rp->pt1.x
eatesdtwruictht Arsepcotse.Slides for .N– ErtT->3p.t51.Cx lient Profile 5.2.0
{ strCucotpyproigihntt 2p0t014;-20上吗11面?A合s法po的s表e P达t式y 都Lt是d.等价的
struct person
{ char nameE[v1a2l]u;ation only. eated with Achsaprossee.Sxl;ides for .NET 3.5 Client Profile 5.2.0
Ccohpayrricgohlto2r0;04-2011 Aspose Pty Ltd.
eated wi本是th课紧A语程 抓s的p对 核核os心C心e.语S的l言Ei核dv核e心asl心u,foa的r在ti.o讲N理n述E解oTn并了l3y非.核.5面心C面l的ie俱n核t到心P,r基o而f础ile 5.2.0 上C讲o解py各ri种gh常t 2用00和4有-2代01表1 A性s的po外se延P。ty一L些td.比较
参的值拷贝给形参
实参:调用E者v提al供u的at参io数n only. eated with Asp形基o参本s:数e.函据S数 类lid定 型e义 无s的 容fo参 置r数 疑.NET 3.5 Client Profile 5.2.0
struct point pt2;
};
11/24/2019
Structures
9
再思考
下面的结构什么意思? – struct something
{
ssttrruuEccvttalssuooammteeiotthhniionnngglyoo.bbjj12;; eated with A} spose.Slides for .NET 3.5 Client Profile 5.2.0
} Cuop;yright 2004-2011 Aspose Pty Ltd.
基本上和struct一样 u.ival、u.fval、u.sval处于同样的地址
sizeof(union xxx)取决于占空间最多的那 个成员变量
11/24/2019
Structures
13
定义自己的类型名
struct person student; /* It works */
抽象数据类型(ADT)在复合数据类型的基础上增加了 对数据的操作
抽象数据类型进而进化为“类” – 这是一个跨时代的进步
11/24/2019
Structures
2
一个问题
在程序里表示一个人(姓名、年龄、性别、身 高、体重……),怎么表示? – char name[12];
unsigned Einvtaluagaeti;on only. eated wi想th–C表Ac定oh示s义papyr多多orsi个个sgee.h数S人xtl;组2i呢d0?e0?s(4f-有o2r些0.1N搞1E笑AT了sp3)o.5seCPliteynLt tPdr.ofile 5.2.0
eated with Ainstpose.Slides fory.eNaEr;T 3.5 Client Profile 5.2.0 Cuuonnpssyiirgginngeehddt 2ii0nn0tt4-20md1oa1nytAhsp::o45se;; Pty Ltd.
};
调整成员顺序可以让结构更紧凑
int year; char month; char day; }; 这样有很多的空间浪费,比如month只可能取值1-12
11/24/2019
Structures
11
位字段
struct person
{
char
name[12];
unsigned unsigned
Eiivnnattluatscioeoxlnoornl::y.22;;
typedef struct tagPerson person;
person student;
/* It works! */
typedef为一种类型定义一个同义词
11/24/2019
Structures
14
C语言的核心学习到此结束
教材P164列出的32个关键字和围绕它们的语 法、符号构成了C语言的核心 – 26个字母以及围绕它们的构词法、语法构成了英
下C面o的py的ri呢gh?t 2004-2011 Aspose Pty Ltd.
– struct something
{ char name[10]; struct something* pOtherObj;
}
11/24/2019
Structures
10
位字段
想表达人的姓名、性别、肤色、出生年、月、日,都 定义什么类型的成员变量?
–Csotrpuycrtigpherts2o0n是04一-个2类01型1 Aspose Pty Ltd.
struct person students[4];
students[0].name students[0].age students[0].sex – 它们都是变量,一般称为结构的成员变量
–C可o以p互yr相ig赋h值t 2004-2011 Aspose Pty Ltd. 所以做为函数的参数时,是传值调用
– 可以& – 不可能参与运算 它的成员个个也都是如假包换的变量 面向对象和数据库是struct的思想的发展
11Байду номын сангаас24/2019
Structures
7
结构指针
struct point
– 常见的可以产E生va数lu值at结io果n的o运nl算y.和操作 eated with Asp算o术s、e.逻S辑lid、e位s运fo算r等.NET 3.5 Client Profile 5.2.0
Cop?有yr返:i、g回h&值、t的2*等函00数4-2011 Aspose Pty Ltd.
11/24/2019
Structures
4
结构(Structure)的内存占用
一个结构变量的成员变量在内存中是相邻的 整个结构变量的将占用多少内存呢?
– 是所有成员变量的内存总和吗?
– 我们可以用sEivzaeloufa来ti获on得o结n构ly的. 大小 eated with– A事s实p上os,e.结Sl构id所es占f的or实.N际E空T间3一.5般C是l按ie照nt机P器ro字file 5.2.0
相关主题