首页 ios开发中的C语言学习—— 结构体简介(烟台杰瑞教育IOS培训部原创)

ios开发中的C语言学习—— 结构体简介(烟台杰瑞教育IOS培训部原创)

举报
开通vip

ios开发中的C语言学习—— 结构体简介(烟台杰瑞教育IOS培训部原创)HYPERLINK"http://www.jerehedu.com/"烟台杰瑞教育科技有限公司(HYPERLINK"http://www.jerehedu.com/"IOS开发培训部)版权所有------HYPERLINK"http://www.jerehedu.com/"杰瑞教育(HYPERLINK"http://www.jerehedu.com"www.jerehedu.com)专注IT技能培训打造一流HYPERLINK"http://www.jerehedu.com/"人才服务平...

ios开发中的C语言学习—— 结构体简介(烟台杰瑞教育IOS培训部原创)
HYPERLINK"http://www.jerehedu.com/"烟台杰瑞教育科技有限公司(HYPERLINK"http://www.jerehedu.com/"IOS开发培训部)版权所有------HYPERLINK"http://www.jerehedu.com/"杰瑞教育(HYPERLINK"http://www.jerehedu.com"www.jerehedu.com)专注IT技能培训打造一流HYPERLINK"http://www.jerehedu.com/"人才服务平台------HYPERLINK"http://www.cnblogs.com/jerehedu/p/5192912.html"ios开发中的C语言学习——结构体简介在开发过程中,经常会需要处理一组不同类型的数据,比如学生的个人信息,由姓名、年龄、性别、身高等组成,因为这些数据是由不同数据类型组成的,因此不能用数组 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 示,对于不同数据类型的一组数据,可以采用结构体来进行存储。当然,对于面向对象的语言来说,最好是用类来表示,但是C语言是面向过程的,因此选择用结构体来表示。一.结构体的定义struct结构体名{类型名成员名1;类型名成员名2;......类型名成员名n;}; 二.结构体的变量声明1.先定义结构体类型,再定义变量代码////main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***定义学生信息的结构体*/structstudent{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高};intmain(intargc,constchar*argv[]){//声明结构变量structstudentstudent1;structstudentstudent2;return0;} 三.定义结构体类型的同时定义变量代码////main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***定义学生信息的结构体,并声明两个学生结构变量student1和student12*/structstudent{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高}student1,student2;intmain(intargc,constchar*argv[]){return0;} 四. 直接定义结构体类型变量,省略类型名代码////main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***直接声明两个结构体变量student1和student2*/struct{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高}student1,student2;intmain(intargc,constchar*argv[]){return0;}  五.结构体的嵌套1结构体中可以包含,但是不允许对结构体本身递归使用。代码////main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***定义日期结构体*/structdate{unsignedintyear;unsignedintmonth;unsignedintday;};/***定义学生结构体*/structstudent{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高structdatebirthday;//出生日期(date结构体)};intmain(intargc,constchar*argv[]){return0; 六.结构体的初始化<一>结构体变量可以在声明的时候一次性给多个成员初始化,但是需要注意的是初始化的顺序必须和定义结构体成员的顺序一样,初始化成员的个数是可以少于总成员个数。<二>声明结构变量后,可以采用结构变量名.成员名来为其赋值或取值。<三>声明结构变量后,可以整体接收相同类型的其他结构变量的值。代码///main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***定义日期结构体*/structdate{unsignedintyear;unsignedintmonth;unsignedintday;};/***定义学生结构体*/structstudent{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高structdatebirthday;//出生日期};intmain(intargc,constchar*argv[]){//<一>一次性给多个成员赋值structdatebirth1={1992,1,1};structstudentstudent1={"jredu",21,'f',180.0,birth1};//<二>对单个成员赋值student1.age=20;student1.height=178.0;//<三>相同类型的变量间可进行整体赋值structstudentstudent2=student1;return0;} 七.结构体的使用  结构体是我们自定义的一种数据类型,但是实际上和系统提供给我们的基本数据类型的使用是一样的,因此,除了可以用结构做变量,还可以用结构体做数组、指针、函数。1结构数组  用数组来存储一组结构体类型的变量,比如存放一组学生的结构数组。  在使用结构数组的时候和上面讲的结构变量一样,同样可以通过三种方式来得到结构数组。代码/***<一>先定义结构体*/structstudent{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高};intmain(intargc,constchar*argv[]){//再声明结构数组structstudentstus[10];return0;} 代码/***<二>定义结构体同时直接声明结构数组*/structstudent{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高}stus[10]; 代码/***<三>直接声明结构数组*/struct{charname[100];//姓名unsignedintage;//年龄charsex;//性别doubleheight;//身高}stus[10]; 2指向结构体的指针要想使用指针来间接改变数据,必须用相同类型的指针去指向对象。结构体类型的变量或者数组在使用的时候就需要使用结构体类型的指针。代码////main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***定义结构体*/structstudent{char*name;//姓名unsignedintage;//年龄};intmain(intargc,constchar*argv[]){//声明结构变量structstudentstudent1={"jredu",21};//定义一个结构指针structstudent*ptr=&student1;//访问结构成员,比如得到学生信息//方式1:直接使用结构变量printf("name=%s,age=%u\n",student1.name,student1.age);//方式2:通过指针得到结构变量printf("name=%s,age=%u\n",(*ptr).name,(*ptr).age);//方式3:直接用指针printf("name=%s,age=%u\n",ptr->name,ptr->age);return0;} 3结构体做函数的参数代码////main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***定义结构体*/structstudent{char*name;//姓名unsignedintage;//年龄};voidfunc1(structstudenttempStu);voidfunc2(structstudent*ptrStu);intmain(intargc,constchar*argv[]){//声明结构变量structstudentstudent1={"jredu",21};structstudentstudent2=student1;//调用参数为结构变量的函数func1(student1);printf("student1name=%s\n",student1.name);//调用参数为结构变量的函数func2(&student2);printf("student2name=%s\n",student2.name);return0;}voidfunc1(structstudenttempStu){tempStu.name="apple";}voidfunc2(structstudent*ptrStu){ptrStu->name="apple";} 八、结构体的简化 typedef可以对数据类型进行重命名,因此在定义结构体的时候可以使用它来简化操作。代码////main.c//结构体////Createdbyjereion15-12-27.//Copyright(c)2015年jerehedu.Allrightsreserved.//#include/***定义结构体*/typedefstruct{char*name;//姓名unsignedintage;//年龄}Student;intmain(intargc,constchar*argv[]){//声明结构变量Studentstudent1={"jredu",21};return0;}
本文档为【ios开发中的C语言学习—— 结构体简介(烟台杰瑞教育IOS培训部原创)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
心想事成2022
暂无简介~
格式:doc
大小:45KB
软件:Word
页数:11
分类:成人教育
上传时间:2022-03-22
浏览量:1