高素质编程师湖南大学语言课件第九章.ppt_第1页
高素质编程师湖南大学语言课件第九章.ppt_第2页
高素质编程师湖南大学语言课件第九章.ppt_第3页
高素质编程师湖南大学语言课件第九章.ppt_第4页
高素质编程师湖南大学语言课件第九章.ppt_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第九章 结构体与共用体 9.1 结构体 char name20; char sex; int age; float score; char addr30; ; name num sex age score addr 2字节 2字节 20字节 1字节 4字节 30字节 结构体类型定义描述结构 的组织形式,不分配内存 结构体类型定义的作用域 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 struct student int num; char name20; char sex; int age; float score; char addr30; ; struct student stu1,stu2; 9.2 结构体变量的定义 先定义结构体类型,再定义结构体变量 v一般形式: struct 结构体名 类型标识符 成员名; 类型标识符 成员名; . ; struct 结构体名 变量名表列; 例 #define STUDENT struct student STUDENT int num; char name20; char sex; int age; float score; char addr30; ; STUDENT stu1,stu2; Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 定义结构体类型的同时定义结构体变量 一般形式: struct 结构体名 类型标识符 成员名; 类型标识符 成员名; . 变量名表列; 例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 直接定义结构体变量 一般形式: struct 类型标识符 成员名; 类型标识符 成员名; . 变量名表列; 例 struct int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; 用无名结构体直接定义 变量只能一次 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 说明 v结构体类型与结构体变量概念不同 l类型:不分配内存; 变量:分配内存 l类型:不能赋值、存取、运算; 变量:可以 v结构体可嵌套 v结构体成员名与程序中变量名可相同,不会混淆 v结构体类型及变量的作用域与生存期 例 struct date int month; int day; int year; ; struct student int num; char name20; struct date birthday; stu; numname birthday monthdayyear 例 struct student int num; char name20; struct date int month; int day; int year; birthday; stu; numname birthday monthdayyear Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 9.3 结构体变量的引用 引用规则 v 结构体变量不能整体引用,只能引用变量成员 v可以将一个结构体变量赋值给另一个结构体变量 v结构体嵌套时逐级引用 成员(分量)运算符 优先级: 1 结合性:从左向右 引用方式: 结构体变量名.成员名 例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; stu1.num=10; stu1.score=85.5; stu1.score+=stu2.score; stu1.age+; 例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; printf(“%d,%s,%c,%d,%f,%sn”,stu1); () stu1=101,“Wan Lin”,M,19,87.5,“DaLian”; () 例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; stu2=stu1; ( ) 例 struct student int num; char name20; struct date int month; int day; int year; birthday; stu1,stu2; numname birthday monthdayyear stu1.birthday.month=12; 例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; if(stu1=stu2) () Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 9.4 结构体变量的初始化 形式一: struct 结构体名 类型标识符 成员名; 类型标识符 成员名; . ; struct 结构体名 结构体变量=初始数据; 例 struct student int num; char name20; char sex; int age; char addr30; ; struct student stu1=112,“Wang Lin”,M,19, “200 Beijing Road”; Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 形式二: struct 结构体名 类型标识符 成员名; 类型标识符 成员名; . 结构体变量=初始数据; 例 struct student int num; char name20; char sex; int age; char addr30; stu1=112,“Wang Lin”,M,19, “200 Beijing Road”; Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 形式三: struct 类型标识符 成员名; 类型标识符 成员名; . 结构体变量=初始数据; 例 struct int num; char name20; char sex; int age; char addr30; stu1=112,“Wang Lin”,M,19, “200 Beijing Road”; Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 9.5 结构体数组 结构体数组的定义 三种形式: 形式一: struct student int num; char name20; char sex; int age; ; struct student stu2; 形式二: struct student int num; char name20; char sex; int age; stu2; 形式三: struct int num; char name20; char sex; int age; stu2; num name sex age num name sex age stu0 stu1 25B Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 结构体数组初始化 例 struct int num; char name20; char sex; int age; stu =,; 顺序初始化: struct student int num; char name20; char sex; int age; ; struct student stu =100,“Wang Lin”,M,20, 101,“Li Gang”,M,19, 110,“Liu Yan”,F,19; 例 struct student int num; char name20; char sex; int age; stu =,; 分行初始化: struct student int num; char name20; char sex; int age; ; struct student stu =100,“Wang Lin”,M,20, 101,“Li Gang”,M,19, 110,“Liu Yan”,F,19; 全部初始化时维数可省 结构体数组引用 引用方式: 结构体数组名下标.成员名 struct student int num; char name20; char sex; int age; str3; stu1.age+; strcpy(,”ZhaoDa”); Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 统计后选人选票 struct person char name20; int count; leader3=“Li”,0,“Zhang”,0,”Wang“,0; main() int i,j; char leader_name20; for(i=1;i成员名结构体变量名.成员名 指向运算符 优先级: 1 结合方向:从左向右 例 指向结构体的指针变量 main() struct student long int num; char name20; char sex; float score; stu_1,*p; p= stu_1.num=89101; strcpy(stu_1.name,“Li Lin“); p-sex=M; p-score=89.5; printf(“nNo:%ldnname:%snsex:%cnscore:%fn“, (*p).num,p-name,stu_1.sex,p-score); 例 int n; int *p= *p=10; n=10 struct student stu1; struct student *p= stu1.num=101; (*p).num=101 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 指向结构体数组的指针 例 指向结构体数组的指针 struct student int num; char name20; char sex; int age; stu3=10101,“Li Lin“,M,18, 10102,“Zhang Fun“,M,19, 10104,“Wang Min“,F,20; main() struct student *p; for(p=stu;pnum,p-name,p-sex,p-age); num name sex age stu0 p stu1 stu2 p+1 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 用指向结构体的指针作函数参数 v用结构体变量的成员作参数-值传递 v用指向结构体变量或数组的指针作参数-地址传递 v用结构体变量作参数-多值传递,效率低Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. struct data int a, b, c; ; main() void func(struct data); struct data arg; arg.a=27; arg.b=3; arg.c=arg.a+arg.b; printf(“arg.a=%d arg.b=%d arg.c=%dn“,arg.a,arg.b,arg.c); printf(“Call Func()n“); func(arg); printf(“arg.a=%d arg.b=%d arg.c=%dn“,arg.a,arg.b,arg.c); void func(struct data parm) printf(“parm.a=%d parm.b=%d parm.c=%dn“,parm.a,parm.b,parm.c); printf(“Process.n“); parm.a=18; parm.b=5; parm.c=parm.a*parm.b; printf(“parm.a=%d parm.b=%d parm.c=%dn“,parm.a,parm.b,parm.c); printf(“Return.n“); arg a :27 b: 3 c :30 (main) (func) parm a :27 b: 3 c :30 copy arg a :27 b: 3 c :30 (main) (func) parm a :18 b: 5 c :90 arg a :27 b: 3 c :30 (main) arg a :27 b: 3 c :30 (main) 例 用结构体变量作函数参数 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. struct data int a, b, c; ; main() void func(struct data *parm); struct data arg; arg.a=27; arg.b=3; arg.c=arg.a+arg.b; printf(“arg.a=%d arg.b=%d arg.c=%dn“,arg.a,arg.b,arg.c); printf(“Call Func()n“); func( printf(“arg.a=%d arg.b=%d arg.c=%dn“,arg.a,arg.b,arg.c); void func(struct data *parm) printf(“parm-a=%d parm-b=%d parm-c=%dn“,parm-a,parm-b,parm-c); printf(“Process.n“); parm-a=18; parm-b=5; parm-c=parm-a*parm-b; printf(“parm-a=%d parm-b=%d parm-c=%dn“,parm-a,parm-b,parm-c); printf(“Return.n“); arg a :18 b: 5 c :90 (main) arg a :27 b: 3 c :30 (main) 例 用结构体指针变量作函数参数 arg a :27 b: 3 c :30 (main) (func) parm * arg a :18 b: 5 c :90 (main) (func) parm * Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 9.8 共用体 char ch; float f; ;f ch i 类型定义不分配内存 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 形式一: union data int i; char ch; float f; a,b; 形式二: union data int i; char ch; float f; ; union data a,b,c,*p,d3; 形式三: union int i; char ch; float f; a,b,c; 共用体变量的定义 f ch i f ch i ab 共用体变量定义分配内存, 长度=最长成员所占字节数 共用体变量任何时刻 只有一个成员存在 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 共用体变量引用 v引用方式: 例 a.i=1; a.ch=a; a.f=1.5; printf(“%d”,a.i); (编译通过,运行结果不对) v引用规则 l不能引用共用体变量,只能引用其成员 共用体指针名-成员名共用体变量名.成员名(*共用体指针名).成员名 union data int i; char ch; float f; ; union data a,b,c,*p,d3; a.i a.ch a.f p-i p-ch p-f (*p).i (*p).ch (*p).f d0.i d0.ch d0.f l共用体变量中起作用的成员是最后一次存放的成员 例 union int i; char ch; float f; a; a=1; () l不能在定义共用体变量时初始化 例 union int i; char ch; float f; a=1,a,1.5; () l可以用一个共用体变量为另一个变量赋值 例 float x; union int i; char ch; float f; a,b; a.i=1; a.ch=a; a.f=1.5; b=a; () x=a.f; () Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 将一个整数按字节输出 01100001 01000001 低字节高字节 01000001 01100001 ch0 ch1 运行结果: i=60501 ch0=101,ch1=141 ch0=A,ch1=a main() union int_char int i; char ch2; x; x.i=24897; printf(“i=%on“,x.i); printf(“ch0=%o,ch1=%on ch0=%c,ch1=%cn“, x.ch0,x.ch1,x.ch0,x.ch1); Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 结构体与共用体 v区别: 存储方式不同 struct node char ch2; int k; a; union node char ch2; int k; b; a ch k bch k 变量的各成员同时存在 任一时刻只有一个成员存在 v联系: 两者可相互嵌套 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 结构体中嵌套共用体 name numsexjob class position Li Wang 1011 2086 F M S T 501 prof 循环n次 读入姓名、号码、性别、职务 job=s 真 真 假 假 读入class 读入 position 输出 “输入错” 循环n次 job=s 真假 输出:姓名,号码, 性别,职业,班级 输出:姓名,号码, 性别,职业,职务 job=t struct int num; char name10; char sex; char job; union int class; char position10; category; person2; Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例共用体中嵌套结构体,机器字数据与字节数据的处理 00010010 00110100 低字节高字节 00110100 00010010 low high 0x1234 00010010 11111111 低字节高字节 11111111 00010010 low high 0x12ff struct w_tag char low; char high; ; union u_tag struct w_tag byte_acc; int word_acc; u_acc; word_acc byte_acc.low byte_acc.high u_acc Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 9.10 用typedef定义类型 功能:用自定义名字为已有数据类型命名 类型定义简单形式: typedef type name; 例 typedef int INTEGER; 类型定义语句关键字已有数据类型名 用户定义的类型名 例 typedef float REAL; 类型定义后,与已有类型一样使用 例 INTEGER a,b,c; REAL f1,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论