8.结构体.pptVIP

  • 1
  • 0
  • 约 32页
  • 2016-12-21 发布于江苏
  • 举报
结构体 任课教师:谭爱平 tangap@ 第八章 结构体 8.1 概述 8.2 定义结构体变量的方法 8.3 结构体变量的初始化 8.4 结构体变量的引用 8.5 结构体数组 8.6 指向结构体数据的指针 8.7 用指针处理链表 8.1 概述 8.2 定义结构体变量的方法 8.3 结构体变量的初始化 8.4 结构体变量的引用 8.5 结构体数组 8.6 指向结构体数据的指针 8.7 用指针处理链表 在实际问题中我们常需要把不同类型的几个数据组合起来, 构成 一个整体。如一个公司职员的个人信息, 或学校中教师和学生的信息。 以学生信息为例, 它可能包括学生的学号、班级、姓名、性别、年龄、 成绩等。这时原有的那些数据类型就显的有点无能为力了,所以引入 一种新的数据类型---- 结构体。 结构体是由一些逻辑相关, 但数据类型不同的分量组成的一组数据。 注意: 用户需要先定义 结构体类型, 之后才能 定义结构体变量 注意不要忘了分号 成员列表 结构体类型定义形式: struct 结构体类型名 { 数据类型 成员名1; 数据类型 成员名2; : : 数据类型 成员名n; } ; 关键字 用户定义 的标识符 一、 定义结构体变量 1. 先定义结构体类型, 再定义变量 struct student { char name[10] ; int age ; float s1 , s2 ; } ; struct student st1 , st2 ; st1 st2 name age s1 s2 name age s1 s2 结构体变量st1和st2各自都 需要20个字节的存储空间 2. 定义结构体类型同时定义变量 struct student { char name[10] ; int age ; float s1 , s2 ; } st1 , st2 ; 3. 直接定义结构体变量 struct { char name[10] ; int age ; float s1 , s2 ; } st1 , st2 ; 4. 说明: (1) 结构体变量具有结构体类型的一切特征 在内存中结构体变量占有一片连续的存储单元 存储单元的字节数可用sizeof 运算符算出 printf(“%d\n” , sizeof(struct student) ) ; printf(“%d\n” , sizeof(st1) ) ; (2) 结构体类型可以嵌套定义 例: struct date { int year ; int month ; int day ; } ; struct stud { char name[10] ; struct date birthday ; float s1 , s2 ; } ; 或 : struct stud { char name[10] ; struct date { int year ; int month ; int day ; } birthday ; float s1 , s2 ; } ; struct student { char name[10] ; int age ; float score1 , score2 ; } st1={“ Mary”, 21, 78, 86} ; struct stud { char name[10] ; struct date birthday ; float score1 , score2 ; } ; struct stud st2={ “John” , 1980 , 11 , 23 , 89 , 95 } ; struct student {

文档评论(0)

1亿VIP精品文档

相关文档