《高级语言程序设计教学课件》第9章.ppt
《《高级语言程序设计教学课件》第9章.ppt》由会员分享,可在线阅读,更多相关《《高级语言程序设计教学课件》第9章.ppt(44页珍藏版)》请在三一办公上搜索。
1、1,第九章 结构体,2,学习目标,结构体的概念结构体类型和结构体变量的差别运算符“.”和“-”结构体数组、结构体指针,3,9.1 结构体,问题:我们要处理一个学生的信息,包括:学号、姓名、性别、年龄、成绩、地址按照以往定义变量的方式,我们可以这样定义:,struct student int num;char name20;char sex;int age;float score;char addr50;,结构体,4,结构类型,【例】平面上点(x,y)的表示,struct point float x;float y;,关键字,结构体名,结构体成员,5,定义一个结构体的一般形式为:注意:1.结构体
2、类型不分配空间,是一种数据类型;2.结构体成员的类型可以不同,struct 结构名 数据类型 成员1;数据类型 成员n;,6,struct student int num;char name20;char sex;int age;float score;char addr50;,结构体类型定义描述结构的组织形式,不分配内存,7,【例】定义点结构变量并保存平面点(4,3),struct point float x;float y;struct point point1;point1.x=4;point1.y=3;,8,定义结构体变量的三种形式,形式1:先定义类型,再定义变量,struct 结构名
3、 数据类型 成员1;数据类型 成员n;struct 结构名 结构变量1,结构变量2;,struct point float x;float y;struct point point1,point2;,9,形式2:在定义类型时,定义变量,struct 结构名 数据类型 成员1;数据类型 成员 n;结构变量1,结构变量2;,struct point float x;float y;point1,point2;,10,形式3:无类型名的变量定义,注意第三种定义形式省略了结构名,在此定义语句后面无法再定义这个类型的其它结构变量,struct 数据类型 成员1;数据类型 成员 n;结构变量1,结构变量2
4、;,struct float x;float y;point1,point2;,结构名,11,说明结构体类型与结构体变量概念不同类型:不分配内存 变量:分配内存类型:不能赋值、存取、运算 变量:可以结构体可嵌套结构体成员名与程序中变量名可同,不会混淆,例 struct date int month;int day;int year;struct student int num;char name20;struct date birthday;stu;,例 struct student int num;char name20;struct date int month;int day;int y
5、ear;birthday;stu;,例 main()struct student int num;char name20;stu;int num;num=0;stu.num=1;,12,【例9-1】结构变量初始化,#include main()struct student long int num;char name20;char addr20;a=89031,Li Lin,123 Beijing Road;printf(No.:%ldnname:%snaddress:%sn,a.num,a.name,a.addr);,初值表,13,初始化的一般形式,在定义结构变量的时候对其进行初始化;初始化
6、表由 括起;逗号隔开的数据对应赋值给结构变量的每个成员,14,结构体变量的引用,引用规则结构体变量不能整体进行输入、输出、赋值、运算引用方式:结构体变量名.成员名可以将一个结构体变量赋值给另一个结构体变量结构体嵌套时逐级引用,struct student int num;char name20;struct date int month;int day;int year;birthday;stu1,stu2;stu1.birthday.month=12;,15,【例】成员变量引用,struct student int num;char name20;char sex;int age;float
7、 score;char addr30;stu1,stu2;,stu1=101,“Wan Lin”,M,19,87.5,“DaLian”;printf(“%d,%s,%c,%d,%f,%sn”,stu1);if(stu1=stu2)stu1.name=“WangLin”;,stu1.num=101;strcpy(stu1.name,“Wang Lin”);stu1.sex=M;Stu1.age=19;stu1.score=87.5;strcpy(stu1.addr,“DaLian”;stu2=stu1;,16,【例】分别定义表示直线和矩形的结构体,struct point float x;flo
8、at y;struct line struct point startp;struct point endp;,struct rect struct point pt1;struct point pt2;struct point pt3;struct point pt4;,17,常见编程错误,错例1,struct stu int no;char name10;float score;=1,“wangbin”,98;,错误分析:struct stu是结构类型,不占内存空间,不能对其赋值。,18,错例2,struct stu int no;char name10;float score;stu s
9、;,错误分析:1)结构体类型定义之后“”后必须加“;”2)结构体类型名为struct stu,不是stu;申明变量应为:struct stu s;,19,错例3,struct stuint no;char name10;float score;struct stu s;scanf(“%d%s%f”,s);,错误分析:不能整体读入结构体变量值,应为 scanf(“%d%s%f”,20,错例4,struct stuint no;char name10;float score;struct stu s,*p;scanf(“%d%s%f”,错误分析:1)p是结构指针变量,使用之前应赋初值;2)p访问成
10、员的方式应为p-no,p-name,p-score;3)p-name为数组名,读入一个字符串时,前面不能加“&”。,21,9.2 结构变量作为函数参数,【例9-2】平面上两点之间的距离,(x1,y1),(x2,y2),22,#include#include struct point float x;float y;float GetDis(struct point,struct point);,23,main()struct point pt1,pt2;printf(Please input pt1:);scanf(%f%f,24,定义计算两点之间距离的函数,float GetDis(stru
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 高级语言程序设计教学课件 高级 语言程序设计 教学 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-5904932.html