C语言chap09 结构

C语言chap09 结构

ID:43309741

大小:220.00 KB

页数:38页

时间:2019-10-08

C语言chap09 结构_第1页
C语言chap09 结构_第2页
C语言chap09 结构_第3页
C语言chap09 结构_第4页
C语言chap09 结构_第5页
资源描述:

《C语言chap09 结构》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、Chap9结构9.1构建手机通讯录9.2结构变量9.3结构数组9.4结构指针本章要点什么是结构?结构与数组有什么差别?有几种结构的定义形式,它们之间有什么不同?什么是结构的嵌套?什么是结构变量和结构成员变量,如何引用结构成员变量?结构变量如何作为函数参数使用?什么是结构数组,如何定义和使用结构数组?什么是结构指针,它如何实现对结构分量的操作?结构指针是如何作为函数的参数的?9.1构建手机通讯录9.1.1程序解析9.1.2结构的概念与定义9.1.3结构的嵌套定义9.1.1程序解析例9-1构建简单的手机通讯录联系人的基本信息:姓名、年龄和联

2、系电话最多容纳50名联系人的信息具有新建和查询功能9.1.1程序解析-程序结构程序结构主函数main:程序的总体控制函数new_friend:新建联系人功能函数search_friend:查询联系人功能main()new_friend()search_friend()程序解析-数据类型/变量数据类型/变量结构类型structfriends_list:在程序首部定义,其中的成员分别代表联系人的基本信息structfriends_list{charname[10];/*姓名*/intage;/*年龄*/chartelephone[13];/

3、*联系电话*/};结构数组friends:每个元素就是一个结构变量,对应一个联系人structfriends_listfriends[50];程序解析-全局变量/函数参数全局变量Count:记录当前的联系人总数函数new_friend和search_friend的参数之一是结构数组:voidnew_friend(structfriends_listfriends[]);voidsearch_friend(structfriends_listfriends[],char*name);结构数组名作为函数实参与普通数组名作函数参数一样,将数组

4、首地址传递给函数形参程序解析-源程序#include#include/*手机通讯录结构定义*/structfriends_list{charname[10];/*姓名*/intage;/*年龄*/chartelephone[13];/*联系电话*/};intCount=0;/*全局变量记录当前联系人总数*/voidnew_friend(structfriends_listfriends[]);voidsearch_friend(structfriends_listfriends[],char*nam

5、e);源程序intmain(void){intchoice;charname[10];structfriends_listfriends[50];/*包含50个人的通讯录*/do{printf("手机通讯录功能选项:1:新建2:查询0:退出");printf("请选择功能:");scanf("%d",&choice);switch(choice){case1:new_friend(friends);break;case2:printf("请输入要查找的联系人名:");scanf("%s",name);search_friend(fr

6、iends,name);break;case0:break;}}while(choice!=0);printf("谢谢使用通讯录功能!");return0;}源程序/*新建联系人*/voidnew_friend(structfriends_listfriends[]){structfriends_listf;if(Count==50){printf("通讯录已满!");return;}printf("请输入新联系人的姓名:");scanf("%s",f.name);printf("请输入新联系人的年龄:");scanf("%d"

7、,&f.age);printf("请输入新联系人的联系电话:");scanf("%s",f.telephone);friends[Count]=f;Count++;}源程序/*查询联系人*/voidsearch_friend(structfriends_listfriends[],char*name){inti,flag=0;if(Count==0){printf("通讯录是空的!");return;}for(i=0;i

8、lag=1;break;}if(flag){printf("姓名:%st",friends[i].name);printf("年龄:%dt",friends[i].age);printf("电话:%s

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。