结构体共用体和用户定义的数据类型

上传人:宝路 文档编号:50056856 上传时间:2018-08-06 格式:PPT 页数:38 大小:438.15KB
返回 下载 相关 举报
结构体共用体和用户定义的数据类型_第1页
第1页 / 共38页
结构体共用体和用户定义的数据类型_第2页
第2页 / 共38页
结构体共用体和用户定义的数据类型_第3页
第3页 / 共38页
结构体共用体和用户定义的数据类型_第4页
第4页 / 共38页
结构体共用体和用户定义的数据类型_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《结构体共用体和用户定义的数据类型》由会员分享,可在线阅读,更多相关《结构体共用体和用户定义的数据类型(38页珍藏版)》请在金锄头文库上搜索。

1、全国计算机等级考试二级教程全国计算机等级考试二级教程CC语言程序设计语言程序设计计算中心- NEU Computing Center敖志广敖志广东北大学计算机中心 2 1 typedef说明一种新的类型名typedef int INTEGER;INTEGER m,n;int m,n;typedef char* CHAR;CHAR p;char* p;东北大学计算机中心 3 2 定义在实际问题中我们常需要把不同类型的几个数据组合起来, 构成 一个整体。如一个公司职员的个人信息, 或学校中教师和学生的信息。 以学生信息为例, 它可能包括学生的学号、班级、姓名、性别、年龄、 成绩等。这时原有的那些数

2、据类型就显的有点无能为力了,所以引入 一种新的数据类型- 结构体。 结构体是由一些逻辑相关, 但数据类型不同的分量组成的一组数据。注意: 用户需要先定义 结构体类型, 之后才能 定义结构体变量注意不要忘了分号成员列表结构体类型定义形式: struct 结构体类型名 数据类型 成员名1;数据类型 成员名2;: :数据类型 成员名n; ;关键字用户定义 的标识符东北大学计算机中心 4 一、 定义结构体变量 1. 先定义结构体类型,再定义变量struct student char name10 ;int age ;float s1 , s2 ; ;struct student st1 , st2 ;

3、st1st2nameages1s2 nameages1s2结构体变量st1和st2各自都 需要20个字节的存储空间东北大学计算机中心 5 2. 定义结构体类型同时定义变量struct student char name10 ;int age ;float s1 , s2 ; st1 , st2 ;3. 直接定义结构体变量struct char name10 ;int age ;float s1 , s2 ; st1 , st2 ; 4. 说明: (1) 结构体变量具有结构体类型的一切特征在内存中结构体变量占有一片连续的存储单元 存储单元的字节数可用sizeof 运算符算出printf(“%dn

4、” , sizeof(struct student) ) ;printf(“%dn” , sizeof(st1) ) ; 东北大学计算机中心 6 (2) 结构体类型可以嵌套定义例: struct date int year ;int month ;int day ; ;struct stud char name10 ;struct date birthday ;float s1 , s2 ; ;或 : struct stud char name10 ;struct date int year ; int month ;int day ; birthday ;float s1 , s2 ; ;东

5、北大学计算机中心 7 3 结构体变量的初始化struct student char name10 ;int age ;float score1 , score2 ; st1=“ Mary”, 21, 78, 86 ;struct stud char name10 ;struct date birthday ;float score1 , score2 ; ; struct stud st2= “John” , 1980 , 11 , 23 , 89 , 95 ;struct student char name10 ;int age ;float score1 , score2 ; ; stru

6、ct student st3; st3=“ Mary”, 21, 78, 86 ;这是初始化, 正确错误, C不允许 这样赋值东北大学计算机中心 8 4 结构体变量的引用1. 引用结构体变量中的成员 格式: 结构体变量名. 成员名 struct student char name10 ;int age ;float s1 , s2 ; ;注意: 一般是对结构体变量的各个成员分别进行赋值st1 = “Mary”, 21 , 78 , 86 ; 这样的赋值是不允许的struct student st1 ; st1. name = “Mary” ; st1. age = 21 ; st1. s1 =

7、 78 ; st1. s2 = 86 ; 东北大学计算机中心 9 4.使用结构体n结构成员的引用结构作为若干成员的集合是一个整体n可对结构整体进行操作n可访问结构中的每个成员使用结构中成员的方法n结构变量名.成员名称运算符“.”的含义是访问结构中的成员 “.”操作的优先级最高 结合性为从左到右n指针变量名-成员名在结构体指针变量情况下使用运算符“-”东北大学计算机中心 10 struct date int year ;int month ;int day ; ; struct stud char name10 ;int age ;struct date birthday;float s1 ,

8、s2 ; ;struct stud st2 ; int age , year ; st2. name = “John” ; st2. age = 20 ; st2. birthday. year = 1980 ; st2. birthday. month = 11 ; st2. birthday. day = 23 ; st2. s1 = 89 ; st2. s2 = 95 ; age = 24 ; year = 2000 ;可以定义与结构体变量成员名相同名字的变量 它们之间不会发生混乱东北大学计算机中心 11 2. 相同类型的结构体变量可以进行整体赋值 struct date int yea

9、r ;int month ;int day ; ; struct stud char name10 ;int age ;struct date birthday;float s1 , s2 ; ;struct stud st1, st2, st3; st1. name = “John” ; st1. age = 20 ; st1. birthday.year = 1980 ; st1. birthday.month = 11 ; st1. birthday.day = 23 ; st1. s1 = 89 ; st1. s2 = 95 ;st2=st1;st3. name=“Mary”; st3

10、. age=20; st3. birthday=st1. birthday; st3. s1 = 76; st3. s2 = 85;注意要正确赋值的条件 是变量st1已经有了数初值东北大学计算机中心 12 3. 结构体变量的输入 输出C语言不允许结构体变量整体进行输入和输出, 只能对结构体变量的成员进行输入和输出 gets( st1. name ) ; scanf( “%d%d%d”, scanf ( “%d%f%f”, puts( s1. name ) ; printf( “%4d”, st1. age ); printf( “%d .%d .%d”, st1. birthday. year

11、 , st1. birthday. month , st1. birthday. day ) ; printf(“%5.2f %5.2fn”, st1. s1 , s1t. s2 ) ; 东北大学计算机中心 13 5 结构体数组一、 结构体数组的定义1. 先定义结构体类型 再定义结构体数组 struct student char name10 ;int age ;float s1 , s2 ; ;struct student st6 ;2. 定义结构体类型的同时定义数组struct student char name10 ;int age ;float s1 , s2 ; st6 ;3. 直接

12、定义结构体数组struct char name10 ;int age ;float s1 , s2 ; st6 ;东北大学计算机中心 14 二、结构体数组的初始化将每个数组元素的数据用花括号 括起来 struct student char name10 ;int age ;float s1 , s2 ; ; struct student st3= “Mary”, 21, 78, 86, “Alex”, 20, 90, 80 , “Mike”,19, 75, 68 ;Mary217886Alex209080Mike197568st0 st1 st2 东北大学计算机中心 15 2. 数组元素之间可

13、以整体赋值也可以将一个元素赋给一个相同类型的结构体变量struct student x , st3= “Mary”,21,78,86, “Alex”, ;st2 = st0 ; x = st1 ; 3. 只能对数组元素的成员进行输入和输出 gets( st2. name ) ; scanf(“%d” , scanf(“%f%f ”, puts( st0. name ); printf(“%4d%5.2f %5.2fn”, st0. age , st0. s1 , st0. s2) ;都是结构体变量的整体赋值三、 结构体数组的引用 1. 引用某个数组元素的成员 例: puts( st0. nam

14、e ) ;printf(“%d, %d”, st1. age , st1. s1 ) ;东北大学计算机中心 16 例: 有30名学生, 输入每个学生信息包括学号、姓名、成绩,要求找出成绩最高者,并输出他的信息 #include #define N 30 void main( ) struct student int n ;char name10 ;int score ;struct student stN;int i , m ;int max ;for ( i=0 ; i max ) max=sti.score; m=i; printf(“%4d”, stm.n ) ;printf(“%10s ”, stm.name );printf(“%5d ”, stm.score); 东北大学计算机中心 17 例: 按成绩对学生信息进行从高到底的排序 #include #define N 30void main( ) struct stud int n ;char name10 ;int s ;struct s

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

当前位置:首页 > 中学教育 > 教学课件

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