![高素质编程师语言课件第九章_第1页](http://file4.renrendoc.com/view/9fc72eb83339824aa1eaa4d85da8432f/9fc72eb83339824aa1eaa4d85da8432f1.gif)
![高素质编程师语言课件第九章_第2页](http://file4.renrendoc.com/view/9fc72eb83339824aa1eaa4d85da8432f/9fc72eb83339824aa1eaa4d85da8432f2.gif)
![高素质编程师语言课件第九章_第3页](http://file4.renrendoc.com/view/9fc72eb83339824aa1eaa4d85da8432f/9fc72eb83339824aa1eaa4d85da8432f3.gif)
![高素质编程师语言课件第九章_第4页](http://file4.renrendoc.com/view/9fc72eb83339824aa1eaa4d85da8432f/9fc72eb83339824aa1eaa4d85da8432f4.gif)
![高素质编程师语言课件第九章_第5页](http://file4.renrendoc.com/view/9fc72eb83339824aa1eaa4d85da8432f/9fc72eb83339824aa1eaa4d85da8432f5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第九章结构体与共用体9.1结构体结构体是一种构造数据类型用途:把不同类型的数据组合成一个整体-------自定义数据类型结构体类型定义struct[结构体名]{
类型标识符成员名;类型标识符成员名;
…………….};成员类型可以是基本型或构造型struct是关键字,不能省略合法标识符可省:无名结构体Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.第九章结构体与共用体9.1结构体struct例struct
student{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};namenumsexagescoreaddr2字节2字节20字节1字节4字节30字节……..结构体类型定义描述结构的组织形式,不分配内存结构体类型定义的作用域Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例structstudentnamenumse例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};
structstudentstu1,stu2;9.2结构体变量的定义先定义结构体类型,再定义结构体变量一般形式:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….};struct结构体名变量名表列;例#defineSTUDENTstructstudent
STUDENT{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};
STUDENTstu1,stu2;Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例structstudent9.2结构体变量定义结构体类型的同时定义结构体变量一般形式:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….}变量名表列;例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.定义结构体类型的同时定义结构体变量struct结构直接定义结构体变量一般形式:struct{
类型标识符成员名;类型标识符成员名;
…………….}变量名表列;例struct{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;用无名结构体直接定义变量只能一次Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.直接定义结构体变量struct例struct用无名说明结构体类型与结构体变量概念不同类型:不分配内存;变量:分配内存类型:不能赋值、存取、运算;变量:可以结构体可嵌套结构体成员名与程序中变量名可相同,不会混淆结构体类型及变量的作用域与生存期例structdate{intmonth;intday;intyear;};structstudent{intnum;charname[20];
structdatebirthday;}stu;numnamebirthdaymonthdayyear例structstudent{intnum;charname[20];
structdate{intmonth;intday;intyear;}birthday;}stu;numnamebirthdaymonthdayyearEvaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.说明例structdatenumnamebirthda9.3结构体变量的引用引用规则结构体变量不能整体引用,只能引用变量成员可以将一个结构体变量赋值给另一个结构体变量结构体嵌套时逐级引用成员(分量)运算符优先级:1结合性:从左向右引用方式:结构体变量名.成员名例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;stu1.num=10;stu1.score=85.5;stu1.score+=stu2.score;stu1.age++;例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;printf(“%d,%s,%c,%d,%f,%s\n”,stu1);()stu1={101,“WanLin”,‘M’,19,87.5,“DaLian”};()例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;stu2=stu1;()例structstudent{intnum;charname[20];structdate{intmonth;intday;intyear;}birthday;}stu1,stu2;numnamebirthdaymonthdayyearstu1.birthday.month=12;例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;if(stu1==stu2)……..()Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.3结构体变量的引用可以将一个结构体变量赋值给另一个结构9.4结构体变量的初始化形式一:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….};struct结构体名结构体变量={初始数据};例structstudent{intnum;charname[20];charsex;intage;charaddr[30];};structstudentstu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.4结构体变量的初始化struct结构体名例形式二:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….}结构体变量={初始数据};例structstudent{intnum;charname[20];charsex;intage;charaddr[30];}stu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.形式二:struct结构体名例struct形式三:struct{
类型标识符成员名;类型标识符成员名;
…………….}结构体变量={初始数据};例struct{intnum;charname[20];charsex;intage;charaddr[30];}stu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.形式三:struct例structEvaluatio9.5结构体数组结构体数组的定义三种形式:形式一:
structstudent{intnum;charname[20];charsex;intage;};structstudentstu[2];形式二:structstudent{intnum;charname[20];charsex;intage;}stu[2];形式三:struct{intnum;charname[20];charsex;intage;}stu[2];numnamesexagenumnamesexagestu[0]stu[1]25BEvaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.5结构体数组形式一:形式二:形式三:numname结构体数组初始化例struct{intnum;charname[20];charsex;intage;}stu[]={{……},{……},{……}};顺序初始化:structstudent{intnum;charname[20];charsex;intage;};structstudentstu[]={100,“WangLin”,‘M’,20,101,“LiGang”,‘M’,19,110,“LiuYan”,‘F’,19};例structstudent{intnum;charname[20];charsex;intage;}stu[]={{……},{……},{……}};分行初始化:structstudent{intnum;charname[20];charsex;intage;};structstudentstu[]={{100,“WangLin”,‘M’,20},{101,“LiGang”,‘M’,19},{110,“LiuYan”,‘F’,19}};全部初始化时维数可省结构体数组引用引用方式:结构体数组名[下标].成员名structstudent{intnum;charname[20];charsex;intage;}str[3];stu[1].age++;strcpy(stu[0].name,”ZhaoDa”);Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.结构体数组初始化例struct顺序初始化:例str例统计后选人选票structperson{charname[20];intcount;}leader[3]={“Li”,0,“Zhang”,0,”Wang“,0};main(){inti,j;charleader_name[20];
for(i=1;i<=10;i++){scanf("%s",leader_name);
for(j=0;j<3;j++) if(strcmp(leader_name,leader[j].name)==0) leader[j].count++;}for(i=0;i<3;i++)printf("%5s:%d\n",leader[i].name,leader[i].count);}namecountLiZhangWang000Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例统计后选人选票structpersonnamecou9.6结构体和指针指向结构体变量的指针定义形式:struct结构体名*结构体指针名;例structstudent*p;使用结构体指针变量引用成员形式存放结构体变量在内存的起始地址numnamesexagestupstructstudent{intnum;charname[20];charsex;intage;}stu;structstudent*p=&stu;(*结构体指针名).成员名结构体指针名->成员名结构体变量名.成员名指向运算符优先级:1结合方向:从左向右例指向结构体的指针变量main(){structstudent{longintnum; charname[20]; charsex; floatscore;}stu_1,*p;p=&stu_1;stu_1.num=89101;strcpy(stu_1.name,"LiLin");
p->sex='M';p->score=89.5;printf("\nNo:%ld\nname:%s\nsex:%c\nscore:%f\n",
(*p).num,p->name,stu_1.sex,p->score);}例intn;int*p=&n;
*p=10;n=10structstudentstu1;structstudent*p=&stu1;stu1.num=101;(*p).num=101Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.6结构体和指针使用结构体指针变量引用成员形式存放结构体指向结构体数组的指针例指向结构体数组的指针structstudent{intnum;charname[20];charsex;intage;}stu[3]={{10101,"LiLin",'M',18},{10102,"ZhangFun",'M',19}, {10104,"WangMin",'F',20}};main(){structstudent*p;for(p=stu;p<stu+3;p++)printf("%d%s%c%d\n",p->num,p->name,p->sex,p->age);}numnamesexagestu[0]pstu[1]stu[2]p+1Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.指向结构体数组的指针例指向结构体数组的指针struct用指向结构体的指针作函数参数用结构体变量的成员作参数----值传递用指向结构体变量或数组的指针作参数----地址传递用结构体变量作参数----多值传递,效率低Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.用指向结构体的指针作函数参数Evaluationonly.structdata{inta,b,c;};main(){voidfunc(structdata);structdataarg;arg.a=27;arg.b=3;arg.c=arg.a+arg.b;printf("arg.a=%darg.b=%darg.c=%d\n",arg.a,arg.b,arg.c);printf("CallFunc()....\n");
func(arg);printf("arg.a=%darg.b=%darg.c=%d\n",arg.a,arg.b,arg.c);}voidfunc(structdataparm){printf("parm.a=%dparm.b=%dparm.c=%d\n",parm.a,parm.b,parm.c);printf("Process...\n");parm.a=18;parm.b=5;parm.c=parm.a*parm.b;printf("parm.a=%dparm.b=%dparm.c=%d\n",parm.a,parm.b,parm.c);printf("Return...\n");}arga:27b:3c:30(main)(func)parma:27b:3c:30copyarga:27b:3c:30(main)(func)parma:18b:5c:90arga:27b:3c:30(main)arga:27b:3c:30(main)例用结构体变量作函数参数Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.structdataarga:27b:3c:30(mstructdata{inta,b,c;};main(){voidfunc(structdata*parm);structdataarg;arg.a=27;arg.b=3;arg.c=arg.a+arg.b;printf("arg.a=%darg.b=%darg.c=%d\n",arg.a,arg.b,arg.c);printf("CallFunc()....\n");
func(&arg);printf("arg.a=%darg.b=%darg.c=%d\n",arg.a,arg.b,arg.c);}voidfunc(structdata*parm){printf("parm->a=%dparm->b=%dparm->c=%d\n",parm->a,parm->b,parm->c);printf("Process...\n");parm->a=18;parm->b=5;parm->c=parm->a*parm->b;printf("parm->a=%dparm->b=%dparm->c=%d\n",parm->a,parm->b,parm->c);printf("Return...\n");}arga:18b:5c:90(main)arga:27b:3c:30(main)例用结构体指针变量作函数参数arga:27b:3c:30(main)(func)parm****arga:18b:5c:90(main)(func)parm****Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.structdataarga:18b:5c:90(m9.8共用体构造数据类型,也叫联合体用途:使几个不同类型的变量共占一段内存(相互覆盖)共用体类型定义定义形式:union共用体名{
类型标识符成员名;类型标识符成员名;
…………….};例uniondata{inti;charch;floatf;};fchi类型定义不分配内存Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.8共用体union共用体名例uniond形式一:uniondata{inti;charch;floatf;}a,b;形式二:uniondata{inti;charch;floatf;};uniondataa,b,c,*p,d[3];形式三:union{inti;charch;floatf;}a,b,c;共用体变量的定义fchifchiab共用体变量定义分配内存,长度=最长成员所占字节数共用体变量任何时刻只有一个成员存在Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.形式一:形式二:形式三:共用体变量的定义fchifchiab共用体变量引用引用方式:例a.i=1;a.ch=‘a’;a.f=1.5;printf(“%d”,a.i);(编译通过,运行结果不对)
引用规则不能引用共用体变量,只能引用其成员共用体指针名->成员名共用体变量名.成员名(*共用体指针名).成员名uniondata{inti;charch;floatf;};uniondataa,b,c,*p,d[3];a.ia.cha.fp->ip->chp->f(*p).i(*p).ch(*p).fd[0].id[0].chd[0].f共用体变量中起作用的成员是最后一次存放的成员例union{inti;charch;floatf;}a;a=1;()
不能在定义共用体变量时初始化例union{inti;charch;floatf;}a={1,’a’,1.5};()
可以用一个共用体变量为另一个变量赋值例floatx;union{inti;charch;floatf;}a,b;a.i=1;a.ch=‘a’;a.f=1.5;b=a;()x=a.f;()Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.共用体变量引用例a.i=1;引用规则共用体指针名->成例将一个整数按字节输出0110000101000001低字节高字节0100000101100001ch[0]ch[1]运行结果:i=60501ch0=101,ch1=141ch0=A,ch1=amain(){unionint_char{inti;charch[2];}x;x.i=24897;printf("i=%o\n",x.i);printf("ch0=%o,ch1=%o\nch0=%c,ch1=%c\n", x.ch[0],x.ch[1],x.ch[0],x.ch[1]);}Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例将一个整数按字节输出011000010100000结构体与共用体区别:存储方式不同structnode{charch[2];intk;}a;unionnode{charch[2];intk;}b;achkbchk变量的各成员同时存在任一时刻只有一个成员存在联系:两者可相互嵌套Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.结构体与共用体structnodeunionno例结构体中嵌套共用体namenumsexjobclasspositionLiWang10112086FMST501prof循环n次读入姓名、号码、性别、职务job==‘s’真真假假读入class读入position输出“输入错”循环n次job==‘s’真假输出:姓名,号码,性别,职业,班级输出:姓名,号码,性别,职业,职务job==‘t’struct{intnum;charname[10];charsex;charjob;
union{intclass;charposition[10];}category;}person[2];Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例结构体中嵌套共用体namenumsexjobcla例共用体中嵌套结构体,机器字数据与字节数据的处理0001001000110100低字节高字节0011010000010010lowhigh0x12340001001011111111低字节高字节1111111100010010lowhigh0x12ffstructw_tag{charlow;charhigh;};unionu_tag{structw_tagbyte_acc;intword_acc;}u_acc;word_accbyte_acc.lowbyte_acc.highu_accEvaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例共用体中嵌套结构体,机器字数据与字节数据的处理00019.10用typedef定义类型功能:用自定义名字为已有数据类型命名类型定义简单形式:typedeftype
name;例typedefintINTEGER;类型定义语句关键字已有数据类型名用户定义的类型名例typedeffloatREAL;类型定义后,与已有类型一样使用例INTEGERa,b,c;REALf1,f2;inta,b,c;floatf1,f2;说明:1.typedef没有创造新数据类型2.typedef是定义类型,不能定义变量3.typedef与define不同
define
typedef预编译时处理
编译时处理简单字符置换
为已有类型命名Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.10用typedef定义类型例typedefitypedef定义类型步骤按定义变量方法先写出定义体如inti;将变量名换成新类型名如intINTEGER;最前面加typedef如typedefintINTEGER;用新类型名定义变量如INTEGERi,j;例定义数组类型inta[100];intARRAY[100];typedefintARRAY[100];ARRAYa,b,c;inta[100],b[100],c[100];例定义指针类型char*str;char*STRING;typedefchar*STRING;STRINGp,s[10];char*p;
char*s[10];例定义函数指针类型int(*p)();int(*POWER)();typedefint(*POWER)();POWERp1,p2;int(*p1)(),(*p2)();例定义结构体类型structdate{intmonth;intday;intyear;}d;例定义结构体类型structdate{intmonth;intday;intyear;}DATE;例定义结构体类型typedefstructdate{intmonth;intday;intyear;}DATE;例定义结构体类型DATEbirthday,*p;structdate{intmonth;intday;intyear;}birthday,*p;类型定义可嵌套例typedefstructclub{charname[20];intsize;intyear;}GROUP;typedefGROUP*PG;PGpclub;GROUP*pclub;structclub*pclub;GROUP为结构体类型PG为指向GROUP的指针类型Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.typedef定义类型步骤例定义数组类型int第九章结构体与共用体9.1结构体结构体是一种构造数据类型用途:把不同类型的数据组合成一个整体-------自定义数据类型结构体类型定义struct[结构体名]{
类型标识符成员名;类型标识符成员名;
…………….};成员类型可以是基本型或构造型struct是关键字,不能省略合法标识符可省:无名结构体Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.第九章结构体与共用体9.1结构体struct例struct
student{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};namenumsexagescoreaddr2字节2字节20字节1字节4字节30字节……..结构体类型定义描述结构的组织形式,不分配内存结构体类型定义的作用域Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例structstudentnamenumse例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};
structstudentstu1,stu2;9.2结构体变量的定义先定义结构体类型,再定义结构体变量一般形式:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….};struct结构体名变量名表列;例#defineSTUDENTstructstudent
STUDENT{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};
STUDENTstu1,stu2;Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例structstudent9.2结构体变量定义结构体类型的同时定义结构体变量一般形式:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….}变量名表列;例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.定义结构体类型的同时定义结构体变量struct结构直接定义结构体变量一般形式:struct{
类型标识符成员名;类型标识符成员名;
…………….}变量名表列;例struct{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;用无名结构体直接定义变量只能一次Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.直接定义结构体变量struct例struct用无名说明结构体类型与结构体变量概念不同类型:不分配内存;变量:分配内存类型:不能赋值、存取、运算;变量:可以结构体可嵌套结构体成员名与程序中变量名可相同,不会混淆结构体类型及变量的作用域与生存期例structdate{intmonth;intday;intyear;};structstudent{intnum;charname[20];
structdatebirthday;}stu;numnamebirthdaymonthdayyear例structstudent{intnum;charname[20];
structdate{intmonth;intday;intyear;}birthday;}stu;numnamebirthdaymonthdayyearEvaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.说明例structdatenumnamebirthda9.3结构体变量的引用引用规则结构体变量不能整体引用,只能引用变量成员可以将一个结构体变量赋值给另一个结构体变量结构体嵌套时逐级引用成员(分量)运算符优先级:1结合性:从左向右引用方式:结构体变量名.成员名例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;stu1.num=10;stu1.score=85.5;stu1.score+=stu2.score;stu1.age++;例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;printf(“%d,%s,%c,%d,%f,%s\n”,stu1);()stu1={101,“WanLin”,‘M’,19,87.5,“DaLian”};()例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;stu2=stu1;()例structstudent{intnum;charname[20];structdate{intmonth;intday;intyear;}birthday;}stu1,stu2;numnamebirthdaymonthdayyearstu1.birthday.month=12;例structstudent{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}stu1,stu2;if(stu1==stu2)……..()Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.3结构体变量的引用可以将一个结构体变量赋值给另一个结构9.4结构体变量的初始化形式一:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….};struct结构体名结构体变量={初始数据};例structstudent{intnum;charname[20];charsex;intage;charaddr[30];};structstudentstu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.4结构体变量的初始化struct结构体名例形式二:struct结构体名{
类型标识符成员名;类型标识符成员名;
…………….}结构体变量={初始数据};例structstudent{intnum;charname[20];charsex;intage;charaddr[30];}stu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.形式二:struct结构体名例struct形式三:struct{
类型标识符成员名;类型标识符成员名;
…………….}结构体变量={初始数据};例struct{intnum;charname[20];charsex;intage;charaddr[30];}stu1={112,“WangLin”,‘M’,19,“200BeijingRoad”};Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.形式三:struct例structEvaluatio9.5结构体数组结构体数组的定义三种形式:形式一:
structstudent{intnum;charname[20];charsex;intage;};structstudentstu[2];形式二:structstudent{intnum;charname[20];charsex;intage;}stu[2];形式三:struct{intnum;charname[20];charsex;intage;}stu[2];numnamesexagenumnamesexagestu[0]stu[1]25BEvaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.9.5结构体数组形式一:形式二:形式三:numname结构体数组初始化例struct{intnum;charname[20];charsex;intage;}stu[]={{……},{……},{……}};顺序初始化:structstudent{intnum;charname[20];charsex;intage;};structstudentstu[]={100,“WangLin”,‘M’,20,101,“LiGang”,‘M’,19,110,“LiuYan”,‘F’,19};例structstudent{intnum;charname[20];charsex;intage;}stu[]={{……},{……},{……}};分行初始化:structstudent{intnum;charname[20];charsex;intage;};structstudentstu[]={{100,“WangLin”,‘M’,20},{101,“LiGang”,‘M’,19},{110,“LiuYan”,‘F’,19}};全部初始化时维数可省结构体数组引用引用方式:结构体数组名[下标].成员名structstudent{intnum;charname[20];charsex;intage;}str[3];stu[1].age++;strcpy(stu[0].name,”ZhaoDa”);Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.结构体数组初始化例struct顺序初始化:例str例统计后选人选票structperson{charname[20];intcount;}leader[3]={“Li”,0,“Zhang”,0,”Wang“,0};main(){inti,j;charleader_name[20];
for(i=1;i<=10;i++){scanf("%s",leader_name);
for(j=0;j<3;j++) if(strcmp(leader_name,leader[j].name)==0) leader[j].count++;}for(i=0;i<3;i++)printf("%5s:%d\n",leader[i].name,leader[i].count);}namecountLiZhangWang000Evaluationonly.CreatedwithAspose.Slidesfor.NET3.5ClientProfile.Copyright2004-2011AsposePtyLtd.例统计后选人选票structpersonnamecou9.6结构体和指针指向结构体变量的指针定义形式:struct结构体名*结构体指针名;例structstudent*p;使用
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 化妆品销售合同书年
- 机械设备购销合同协议书范本
- 房屋建筑工程保修合同书范本
- 通信工程承包合同模板
- 苏州室内装修合同范本
- 铸件加工合同范本
- 销售员合同协议书
- 数据产业能否促进经济快速发展
- 课程游戏化背景下师幼互动模式的创新研究
- 档案叙事与共情:理论阐释与实证分析
- 复工复产消防安全培训
- 城市道路交通安全评价标准 DG-TJ08-2407-2022
- 统编版高中政治选择性必修2《法律与生活》知识点复习提纲详细版
- 急腹症的诊断思路
- 培训机构安全隐患排查记录(带附件)
- 2024小说推文行业白皮书
- 研究性成果及创新性成果怎么写(通用6篇)
- 特殊感染手术管理考试试题及答案
- 旅馆治安管理制度及突发事件应急方案三篇
- 土地增值税清算底稿中税协版
- 小区绿化养护方案及报价(三篇)
评论
0/150
提交评论