当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

济南大学:《C语言程序设计》课程教学资源(PPT课件)第十一章 结构体(袁宁)

资源类别:文库,文档格式:PPT,文档页数:32,文件大小:393.5KB,团购合买
11.1 概述 11.2 定义结构体变量的方法 11.3 结构体变量的引用 11.4 结构体变量的初始化 11.5 结构体数组 11.6 指向结构体数据的指针 11.7 用指针处理链表
点击下载完整版文档(PPT)

第十一章结构体 ■11概述 ■112定义结构体变量的方法 ■113结构体变量的引用 ■114结构体变量的初始化 115结构体数组 ■116指向结构体数据的指针 ■117用指针处理链表

第十一章 结构体 ◼ 11.1 概述 ◼ 11.2 定义结构体变量的方法 ◼ 11.3 结构体变量的引用 ◼ 11.4 结构体变量的初始化 ◼ 11.5 结构体数组 ◼ 11.6 指向结构体数据的指针 ◼ 11.7 用指针处理链表

111概述 在实际问题中我们常需要把不同类型的几个数据组合起来构成 个整体。如一个公司职员的个人信息,或学校中教师和学生的信息。 以学生信息为例,它可能包括学生的学号、班级、姓名、性别、年龄、 成绩等。这时原有的那些数据类型就显的有点无能为力了,所以引入 种新的数据类型结构体。 结构体是由一些逻辑相关,但数据类型不同的分量组成的一组数据。 结构体类型定义形式 struct结构体类型名用户定义 数据类型成员名1;的标识符 关键字]数据类型成员名2称成员表列 注意:用户需要先定义 结构体类型,之后才能数据类型成员名m 定义结构体变量}注意不要忘了分号

11.1 概述 在实际问题中我们常需要把不同类型的几个数据组合起来, 构成 一个整体。如一个公司职员的个人信息, 或学校中教师和学生的信息。 以学生信息为例, 它可能包括学生的学号、班级、姓名、性别、年龄、 成绩等。这时原有的那些数据类型就显的有点无能为力了,所以引入 一种新的数据类型----结构体。 结构体是由一些逻辑相关, 但数据类型不同的分量组成的一组数据。 注意: 用户需要先定义 结构体类型, 之后才能 定义结构体变量 注意不要忘了分号 称成员表列 结构体类型定义形式: struct 结构体类型名 { 数据类型 成员名1; 数据类型 成员名2; : : 数据类型 成员名n; } ; 关键字 用户定义 的标识符

112定义结构体变量的方法 、定义结构体变量 name 1.先定义结构体类型, age 再定义变量 struct student i char name 10; int age name float sI, S2 age st2 struct student stI, st2 结构体变量st1和st2各自都 需要20个字节的存储空间

11.2 定义结构体变量的方法 一、 定义结构体变量 1. 先定义结构体类型 , 再定义变量 struct student { char name[10] ; int age ; float s1 , s2 ; } ; struct student st1 , st2 ; st1 st2 name age s1 s2 name age s1 结构体变量st1 和st2各自都 s2 需要20个字节的存储空间

2.定义结构体类型同时定义变量3.直接定义结构体变量 struct student struct i char name[10li i char name[10; int age, int age; float sI, S2 3 stI, st2 i float sl, S2; 3 stI, st2 4.说明: (1)结构体变量具有结构体类型的一切特征 在内存中结构体变量占有一片连续的存储单元 存储单元的字节数可用 sizeof运算符算出 printf( %odin", sizeof(struct student)) printf(%od\n", sizeof(stD));

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 或: struct stud i int year; i char name 10 int month struct date int day i int year i int month struct stud i char name 10; int day i struct date birthday 3 birthday i float sI, S2 float sI. s2

(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 ; } ;

113结构体变量的引用 1.引用结构体变量中的成员 格式:结构体变量名.成员名 struct student struct student st1 i char name[10; stl.name=“Mary”; int age i stl age= 21 float s1, s2 st1. s1=78 }; st1.s2=86 注意:一般是对结构体变量的各个成员分别进行赋值 stl={“Mary”,21,78,86};这样的赋值是不允许的

11.3 结构体变量的引用 1. 引用结构体变量中的成员 格式: 结构体变量名. 成员名 struct student { char name[10] ; int age ; float s1 , s2 ; } ; 注意: 一般是对结构体变量的各个成员分别进行赋值 st1 = { “Mary”, 21 , 78 , 86 } ; 这样的赋值是不允许的 struct student st1 ; st1. name = “Mary” ; st1. age = 21 ; st1. s1 = 78 ; st1. s2 = 86 ;

struct date struct stud st2 t int year int age, year int month i st2.name=“John”; int day i t2. age= 20 struct stud t2. birthday year=1980 i char name[101 st2 birthday. month=11; int age st2 birthday day= 23 struct date birthday st2.s1=89 float s1. s2 t2.s2=95 可以定义与结构体变量 age=24; 成员名相同名字的变量year=2000 它们之间不会发生混乱

struct date { int year ; int month ; int day ; } ; struct stud { char name[10] ; int age ; struct date birthday; float s1 , 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 ; 可以定义与结构体变量 成员名相同名字的变量 它们之间不会发生混乱

2.相同类型的结构体变量可以进行整体赋值 struct date struct stud stl st2 st3 i int year st1.name=“John” int month; stl age= 20 int day i stl birthday year =1980 stl birthday month=11 struct stud i char name[10 stl. birthday day=23 st1. sI=89 int age struct date birthday; st1.s2=95 float s1. s2 st2=stI 注意要正确赋值的条件 st3.name=“Mary”; 是变量st1已经有了数据 st3. age=20; st3. birthday=stl birthday st3.s1=76 st3.s2=85

2. 相同类型的结构体变量可以进行整体赋值 struct date { int year ; int month ; int day ; } ; struct stud { char name[10] ; 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. age=20; st3. birthday=st1. birthday; st3. s1 = 76; st3. s2 = 85; 注意要正确赋值的条件 是变量st1已经有了数据

3.结构体变量的输入输出 C语言不允许结构体变量整体进行输入和输出, 只能对结构体变量的成员进行输入和输出 gets( stl name )i scanf(“%d%d%d”,&st1. birthday.year &stl birthday. month, &stl birthday day )i scanf ("%d %f%of', &stl. age, &stl. sl, &stl. S2); puts( sl name )i printf(“%4d”,stl.age) printf(“%d.%d.%d”,stl. birthday.year, stl birthday. month, stl birthday day printf(o5.2f%5.2f\n",stI sl, sIt. s2)

3. 结构体变量的输入输出 C语言不允许结构体变量整体进行输入和输出, 只能对结构体变量的成员进行输入和输出 gets( st1. name ) ; scanf( “%d%d%d”, &st1. birthday. year , &st1. birthday. month , &st1. birthday. day ) ; scanf ( “%d%f%f”, &st1. age , &st1. s1 , &st1. s2 ) ; puts( s1. name ) ; printf( “%4d”, st1. age ); printf( “%d .%d .%d”, st1. birthday. year , st1. birthday. month , st1. birthday. day ) ; printf(“%5.2f %5.2f\n”, st1. s1 , s1t. s2 ) ;

114结构体变量的初始化 struct student struct student i char name[10j; i char name[10li int age int age float scorel. score2 float scorel. score2 }st=“Mary”,21,78,86}; struct student stI 这是初始化,正确st1={“Mary”,2l,78,86}; struct stud i char name[10li 这是赋值,错误 e1, score2."i' struct date birthda C不允许这么做 float score struct stud st2={“John”, 1980,11,23,89,95};

11.4 结构体变量的初始化 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 { char name[10] ; int age ; float score1 , score2 ; } ; struct student st1; 这是初始化, 正确 st1={“ Mary”, 21, 78, 86} ; 这是赋值, 错误 C不允许这么做

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共32页,可试读12页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有