1 / 13

单元 10 项目中结构体的应用 ---- 结构体变量做函数参数

单元 10 项目中结构体的应用 ---- 结构体变量做函数参数. 学习目标: (1) 能力目标 ①能用结构体变量做函数参数在函数之间传递结构体数据。 ②能利用“ .” 运算符和“ ->” 运算符正确引用结构体变量成员。 (2) 知识目标 ①理解结构体变量做函数参数在函数之间传递参数的特点。 ②理解结构体做函数参数时变量成员引用方法。 ③理解结构体变量做函数类型,返回结构体数据的函数。 能力训练: 1. 结构体成员变量做函数参数和结构体变量做函数参数的实际应用。

tave
Download Presentation

单元 10 项目中结构体的应用 ---- 结构体变量做函数参数

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 单元10 项目中结构体的应用----结构体变量做函数参数 • 学习目标: (1)能力目标 ①能用结构体变量做函数参数在函数之间传递结构体数据。 ②能利用“.”运算符和“->”运算符正确引用结构体变量成员。 (2)知识目标 ①理解结构体变量做函数参数在函数之间传递参数的特点。 ②理解结构体做函数参数时变量成员引用方法。 ③理解结构体变量做函数类型,返回结构体数据的函数。 • 能力训练: 1.结构体成员变量做函数参数和结构体变量做函数参数的实际应用。 2.用学生信息结构体变量做函数参数在函数之间传递学生信息。

  2. 单元10 项目中结构体的应用---- 10.1.2 结构体类型的应用 4.结构体变量和结构体指针变量在函数中的应用 结构体变量和结构体指针变量在函数中有非常重要的应用,它们即可以做函数的形参,也可以做函数的实参,还可以做函数类型,返回结构体。 (1)结构体变量和结构体指针变量做函数参数 和简单变量一样,结构体变量、成员变量、结构体指针变量都可以做函数的参数。 ①结构体成员变量做实参。 前面介绍的简单变量作实参一样,属于“值传递”方式,只是要注意形参与实参在类型上要保持一致,如图10.3所示。 图10.3 结构体成员变量做实参数据传递示意图

  3. 单元10 项目中结构体的应用---- 10.1.2 结构体类型的应用 ②结构体变量做参数。 结构体变量做实参,要求形参也是相同类型的结构体变量,参数传递采用的是“值传递”的方式,形参在函数调用期间也要占用内存单元,因此这种传递方式在空间与时间上开销较大,传递过程如图10.4所示。 ③结构体指针变量做函数 结构体指针变量做参数,传递的是结构体变量的首地址,要求形参也是一个能接受地址的相同类型的指针变量,地址传递过程如图10.5所示。 图10.4 结构体变量做实参和形参数据传递示意图 图10.5 结构体指针变量做函数参数地址传递示意图

  4. 单元10 项目中结构体的应用---- 10.1.2 结构体类型的应用 【例10.4】有一个结构体变量stu,内含学生学号,姓名和3门课的成绩,要求在main函数中赋值,在另一个函数output中将它们打印输出。 ①结构体成员变量做实参 程序代码: 1 #include <stdio.h> 2 #include <string.h> 3 #define FORMAT "%d\n%s\n%.1f\n%.1f\n%.1f\n" 4 struct student 5 { 6 int num; 7 char name[20]; 8 float score[3]; 9 }; 10 11 void main() 12 { 13 void output(int,char *,float,float,float); //函数声明 14 struct student stu; 15 stu.num=12345;

  5. 单元10 项目中结构体的应用---- 10.1.2 结构体类型的应用 16 strcpy(stu.name,"Liming"); 17 stu.score[0]=67.5f; 18 stu.score[1]=89.0f; 19 stu.score[2]=78.6f; 20 output(stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]); //结构体成员变量做实参 21 } 22 23 void output(int num,char *pname,float score1,float score2,float score3) 24 { 25 printf(FORMAT,num,pname,score1,score2,score3); 26 } 程序运行结果: 12345 LiLi 67.5 89.0 78.6

  6. 单元10 项目中结构体的应用---- 10.1.2 结构体类型的应用 ②结构体变量做实参 程序代码: 1 #include <stdio.h> 2 #include <string.h> 3 #define FORMAT "%d\n%s\n%f\n%f\n%f\n" 4 struct student 5 { 6 int num; 7 char name[20]; 8 float score[3]; 9 }; 10 void main() 11 { 12 void output(struct student); //函数声明 13 struct student stu; 14 stu.num=12345; 15 strcpy(stu.name,"LiLi"); 16 stu.score[0]=67.5f; 17 stu.score[1]=89.0f; 18 stu.score[2]=78.6f; 19 output(stu); //结构体变量做实参 20 }

  7. 单元10 项目中结构体的应用---- 10.1.2 结构体类型的应用 21 22 void output(struct student stu1) //结构体变量名做形参 23 { printf(FORMAT,stu1.num,stu1.name,stu1.score[0],stu1.score[1], stu1.score[2]); 24 } 程序运行结果同上。 ③结构体指针变量做参数 程序代码: 1 #include <stdio.h> 2 #include <string.h> 3 #define FORMAT "%d\n%s\n%.1f\n%.1f\n%.1f\n" 4 struct student 5 { 6 int num; 7 char name[20]; 8 float score[3]; 9 };

  8. 单元10 项目中结构体的应用---- 10.1.2 结构体类型的应用 10 void main() 11 { 12 void output(struct student *);//函数声明 13 struct student stu; 14 stu.num=12345; 15 strcpy(stu.name,"Liming"); 16 stu.score[0]=67.5f; 17 stu.score[1]=89.0f; 18 stu.score[2]=78.6f; 19 output(&stu); //结构体变量地址做实参 20 } 21 22 void output(struct student *sp)//结构体指针变量做形参 23 { 24 printf(FORMAT,sp->num,sp->name,sp->score[0],sp->score[1], sp->score[2]); 25 } 程序运行结果同上。

  9. 单元10 项目中结构体的应用---- 10.1.3 结构体变量做函数类型 10.1.3 结构体变量做函数类型 结构体变量和其它简单变量一样也可以做函数类型,返回结构体数据,这样可以大大方便程序设计。 【例10.5】将例10.4作如下修改,学生数据输入用input函数实现,output函数将其输出,主函数实现输入输出函数调用。 程序代码: 1 #include <stdio.h> 2 #include <string.h> 3 #define FORMAT "学号:%d\n姓名:%s\n成绩1:%.1f\n成绩2:%.1f\n成绩3:%.1f\n" 4 struct student input();//函数声明 5 void output(struct student *);//函数声明 6 struct student 7 { 8 int num; 9 char name[20]; 10 float score[3]; 11 }; 12

  10. 单元10 项目中结构体的应用---- 10.1.3 结构体变量做函数类型 13 void main() 14 { 15 struct student stu,*sp; 16 sp=&stu; 17 stu=input();//调用输入函数 18 printf("\n输出学生信息\n"); 19 output(sp); //调用输出函数 20 } 21 22 struct student input() 23 { 24 struct student stu,*sp; 25 int i; 26 sp=&stu; 27 printf("请输入学号:"); 28 scanf("%d",&sp->num); 29 printf("请输入姓名:"); 30 scanf("%s",sp->name);

  11. 单元10 项目中结构体的应用---- 10.1.3 结构体变量做函数类型 31 for(i=0;i<3;i++) 32 { 33 printf("请输入第%d门成绩:",i+1); 34 scanf("%f",&sp->score[i]); 35 } 36 return stu; 37 } 38 39 void output(struct student *sp)//结构体指针变量做形参 40 { 41 printf(FORMAT,sp->num,sp->name,sp->score[0],sp->score[1], sp->score[2]); 42 } 程序运行结果: 请输入学号:1001 <回车> 请输入姓名:LiMing <回车> 请输入第1门成绩:98 <回车> 请输入第2门成绩:78 <回车> 请输入第3门成绩:85 <回车>

  12. 单元10 项目中结构体的应用---- 10.1.3 结构体变量做函数类型 输出学生信息 学号:1001 姓名:LiMing 成绩1:98 成绩2:78 成绩3:85 通过上面的实例可以看出,函数声明可以放在函数内,也可以放在函数外,但一定要在调用该函数之前。sp->num表示的是成员变量,取它的地址表示成&sp->num,还有取数组元素地址也要表示成&sp->score[i]。同学们想一想,在上例中输入姓名时,为什么使用的是sp->name,而不是&sp->name?

  13. 单元10 项目中结构体的应用----课后作业与思考 1.填空题: (1)结构体定义中,其成员类型可以是除任何已有类型,也可以是的指针类型,也就是说结构体不允许定义。 2.选择题: (1)设有以下说明语句 struct student { int a; float b; }stutype;则下面的叙述不正确的是。 A.struct是结构体类型的关键字 B.struct student是用户定义的结构体类型 C.stutype是用户定义的结构体类型名 D.a和b都是结构体成员名 3.编程题: (1)用户手机通信收费由月租费,通话费和短信费组成,编写程序分别输入两个人的各项费用,计算两个人的通信费之差。

More Related