C语言-14-结构体 PPT课件

上传人:Lesl****ang 文档编号:165208240 上传时间:2021-02-01 格式:PPT 页数:26 大小:788.50KB
返回 下载 相关 举报
C语言-14-结构体 PPT课件_第1页
第1页 / 共26页
C语言-14-结构体 PPT课件_第2页
第2页 / 共26页
C语言-14-结构体 PPT课件_第3页
第3页 / 共26页
C语言-14-结构体 PPT课件_第4页
第4页 / 共26页
C语言-14-结构体 PPT课件_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《C语言-14-结构体 PPT课件》由会员分享,可在线阅读,更多相关《C语言-14-结构体 PPT课件(26页珍藏版)》请在金锄头文库上搜索。

1、,“结构体”是一种构造类型 一组具有不同数据类型的数据的集合 每个元素称为:成员。 每一个成员可以是一个基本数据类型或者是一个构造类型。 在使用之前必须先定义它,也就是构造它。,结 构 体,P281,struct 结构体名 类型标识符 成员名; 类型标识符 成员名; . ;,成员类型可以是 基本型或构造型,struct是关键字,不能省略,合法标识符 可省:无名结构体,结构体类型定义,如, “构造”一学生登记空表,在该表中有以下成员: 学号(整型或字符型) 姓名(字符型); 性别(字符型); 成绩(整型或实型)。,struct student char num10; char name20; c

2、har sex; int score; ;, 后的分号不能省略,struct student char num10; char name20; char sex; int score1; int score2; int score3; ;,int score3;,P282,结构体类型定义后结构组织的形式:,它所占的内存空间为各个成员所占内存空间之和。,结构体类型定义描述结构的组织形式,不分配内存.,struct student char num10; char name20; char sex; float score; ;,struct 类型名 类型标识符 成员名; 类型标识符 成员名; .

3、 ; struct 类型名 变量名表列;,struct char name20; int num; int age; char sex; float score; st1 ,st2;,struct student char name20; int num; int age; char sex; float score; st1 ,st2;,结构体类型变量的定义,struct student char name20; int num; int age; char sex; float score; ;,struct student st1,st2;,P282,struct 类型名 类型标识符 成

4、员名; 类型标识符 成员名; . 变量名表列;,结构体类型与结构体变量,struct student char name20; int num; int age; char sex; float score; ;,struct student st1,st2;,结构体变量名,结构体类型与结构体变量概念不同 类型:不分配内存; 变量:分配内存 类型:不能赋值、存取、运算; 变量:可以,st1 ,st2;,结构体变量名,结构体类型名,对结构体变量的使用,包括赋值、输入、输出、 运算等都是通过结构体变量的成员来实现的。具有相同类型的结构变量相互可以赋值,引用结构变量成员的形式: 结构变量名.成员名,

5、结构变量stu1的学号:,stu1.num,结构变量stu2的姓名:,stu2.name,struct student char num10; char name20; struct date int month; int day; int year; birthday; stu1,stu2;,结构变量stu1出生的年:,stu1.birthday.year,结构体变量的引用,P284,结构体变量的初始化,在定义结构体变量的同时赋值称之为结构体变量初始化。结构体变量的定义有三种方式,则结构体变量初始化也有三种方式。即:,struct student char num10; char name2

6、0; char sex; float score2; ; struct student stu1=31012112,Wang Lin, M,56, 83;,struct student char num10; char name20; char sex; float score2; stu1=31012112,Wang Lin,M,56, 83;,struct char num10; char name20; char sex; float score2; stu1=31012112,Wang Lin,M,56, 83;,P297,#include #include void main() s

7、truct stu char num10; char name20; char sex; float score; s1,s2; strcpy(s1.num,30333102); strcpy(s1.name,Zhang ping); scanf(%c%f,结构体变量的赋值,结构体成员赋值 字符串赋值,采用输入语句进行成员赋值,结构体变量直接赋值,结构体数组,结构体数组的定义(三种形式):,形式一: struct student char num10; char name20; char sex; int age; ; struct student stu2;,结构体数组就是具有相同结构体类型

8、的变量集合。即stu是一个包含2个元素的结构体数组。,形式二: struct student char num10; char name20; char sex; int age; stu2;,形式三: struct char num10; char name20; char sex; int age; stu2;,P286,结构类型与结构数组,struct student char num10; char name20; char sex; int age; stu3;,stu0.name,stu1.age,引用方式: 结构体数组名下标.成员名,P286,20215 Li Ping M 18

9、 20216 Zhang San M 17 20217 Wang Wu F 19,#define N 30 #include void main() struct STU long num; char name20; int score; ; struct STU aN,t; int i , j , k;,设某班级有学生30名,学期末将C语言程序设计课程的前10名学生的学号、姓名及成绩张榜公布,以下程序输出张榜清单。 请填空。,for(i=0;iak.score) k=j; if(i!=k) _;_;_; printf(Number Name Scoren); for(i=0;i10;i+)

10、printf(%10ld%10s%8dn,ai.num , ai.name , ai.score);,ak=t,t=ai,ai=ak,设某班级有学生30名,学期末将C语言程序设计课程的前10名学生的学号、姓名及成绩张榜公布,以下程序输出张榜清单。 请填空。,#define N 50 #include #include void main() int i,j; struct EX char name10; int num; ;,现有三名同学(Guo、Liu、Hu)竞选班长,其方法通过投票选举出得票最高的同学当选。每人推举一名,选票全部有效,输出三名候选人的得票数。请填空。,P287,struct

11、 EX stu3=Guo,0,Liu,0,Hu,0; char name10; for(i=0;iN;i+) /*本循环完成唱票并统计票数*/ scanf(%s,name) ; for(j=0;j3;j+) if(strcmp(name,stuj.name)=0) stuj.num+ ; printf(Name Depiaoshu:n); for(i=0;i3;i+) printf(%5s%3dn,stui.name,stui.num);,现有三名同学(Guo、Liu、Hu)竞选班长,其方法通过投票选举出得票最高的同学当选。每人推举一名,选票全部有效,输出三名候选人的得票数。,typedef,

12、功能:用自定义名字为已有数据类型(别名)命名 类型定义简单形式: typedef type name;,例 typedef int IG;,类型定义语句关键字,已有数据类型名,用户定义的类型名,例 typedef float RA;,类型定义后,与已有类型一样使用,P315,例 typedef int IG; typedef float RA; IG a,b,c; RA f1,f2;,int a,b,c; float f1,f2;,说明: 1.typedef 不能定义新数据类型 2.typedef 是定义类型,不能定义变量 3.typedef 与 define 不同 4.define 预编译时

13、处理,进行简单字符置换 5.typedef 编译时处理,为已有数据类型命名,它的特点: 简化复杂的类型说明,例:以下程序的输出结果是_。 struct node long x3; int y4; char z10; ; struct node st; void main() printf(%dn,sizeof(st); ,30,typedef struct mu,MT;,MT st;,定义结构体变量 st,指针与结构体,P289,struct 结构体名 类型标识符 成员名; 类型标识符 成员名; . ;,struct 类型名 类型标识符 成员名; 类型标识符 成员名; . ; struct 类

14、型名 *变量名;,struct char name20; int num; int age; char sex; float score; *p1 ,*p2;,struct student char name20; int num; int age; char sex; float score; *p1 ,*p2;,结构体指针变量的定义,struct student char name20; int num; int age; char sex; float score; ;,struct student *p1,*p1;,P289,struct 类型名 类型标识符 成员名; 类型标识符 成员

15、名; . *变量名;,结构体指针变量的引用,定义形式:struct 结构体名 *结构体指针名;,struct student int num; char name20; char sex; int age; stu ;,struct studnt *p;,引用形式: (*结构体指针名).成员名,引用常用形式:结构体指针名-成员,结构体变量,P290,p= float sco; stu3=2,76,4,87,8,56; int i; struct student *p; p=stu; printf(%dt,p+-num); printf(%fn,p-sco); printf(%dt,p-num+); printf(%dn,+p-num); printf(%fn,(+p)-sco); for(i=0;i3;i+) print

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > C/C++资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号