结构体的初始化与基本操作C语言中的结构体的定义方法如下:Structdate{intyear;intmonth;Intday;};(注意最后的分号不能省略)。其中第一行的“date”是该结构体的名称,花括号里面的内容是结构体的成员名,这是声明结构体的一般形式定义结构体变量方法1:Structdate{intyear;intmonth;Intday;};structdatebirthday;Birthday.year//引用成员定义结构体变量方法2:Structdate{intyear;intmonth;Intday;}birthday;例:用结构描述一个人的基本情况。structperson/*定义person结构类型*/{charname[30];charsex;structdatebirth;/*结构的嵌套定义*/}tman;//:1984年7月28日出生的先生:tman.sex=“M”;tman.birth.year=1984;tman.birth.month=7;tman.birth.day=28;例2:输入今天的日期,然后输出该日期。#includestdio.hmain(){structdate/*在函数中定义结构类型date*/{intyear,month,day;};structdatetoday;/*说明结构变量today*/printf(Entertodaydate:);scanf(%d%d%d,&today.year,&today.month,&today.day);printf(Today:%d.%d.%d\n,today.year,today.month,today.day);}练习:输入昨天的日期,然后输出今天的日期。main(){structdate/*在函数中定义结构类型date*/{intyear,month,day;};structdateyesterday;/*说明结构变量*/printf(Enteryesterdaydate:);scanf(%d%d%d,&yesterday.year,&yesterday.month,&yesterday.day);printf(Today:%d.%d.%d\n,yesterday.year,yesterday.month,yesterday.day+1);}例3:结构体变量进行初始化structdate{intyear,month,day;};structperson{charname[14],sex;structdatebirthday;//嵌套定义};对date类型的变量,可以进行初始化:structdatetoday={1992,10,1};又如对变量man的初始化可以用如下形式:structpersonman={“deng,‘M',{1989,3,28}};structstu//结构体数组初始化例4{intnum;CHAR*NAME;CHARsex;floatscore;}pupil[5]={{101,“Tom”,‘M’,45.8},//赋予pupil[0]{102,Mike,'M',62.5},{103,Chris,'F',92.5},{104,Rose,'F',87.6},{105,Nate,'M',58.8}};该代码中的“pupil[5]”称为结构体数组,它属于结构体变量,在定义该变量的同时对它进行了初始化操作。例题6结构体和指针structstu{intnum;CHARsex;floatscore;}pupil[5]={{101,'M',45.8},{102,'M',62.5},{103,'F',92.5},{104,'F',87.6},{105,'M',58.8}};voidavg(structstu*ps)//用指针ps来传递pupil的地址{intc=0,i;floatave,s=0;for(i=0;i5;i++,ps++){s+=ps-score;//s=s+ps.scoreif(ps-score60)c=c+1;}//ps-score=ps.scoreprintf(s=%.3f\n,s);ave=s/5;printf(average=%.3f\ncount=%d\n,ave,c);}Voidmain(){structstu*myps;//myps指向的地址中存储的是结构体myps=pupil;//myps指向pupil[0]的地址avg(myps);}structstu{intnum;CHARsex;floatscore;}pupil[5]voidavg(structstu*p)//用指针ps来传递pupil的地址{intc=0,i;floatave,s=0;for(i=0;i5;i++,){s+=;if(60)c=c+1;}//printf(s=%.3f\n,s);ave=s/5;printf(average=%.3f\ncount=%d\n,ave,c);}Voidmain(){structstu*myps;//myps指向的地址中存储的是结构体myps=pupil;//myps指向pupil[0]的地址,pupil[0]引用一avg(myps);}structstu{intnum;CHARsex;floatscore;}pupil[5]此程序是关于结构体指针变量作函数参数,这样可以提高程序的运行效率,程序中我们定义了一个“stu”的结构体,变量名为“pupil[5]”,并对其进行了初始化,在主函数中定义了一个该结构体的指针ps,将pupil赋值给ps,当函数avg()调用该结构体时,用指针ps来传递pupil的地址,从而,提高了该程序的效率。结构体与指针的结合使用,可以有效的解决现实生活中的很多问题作业1:分析下面程序的功能voidmain(){structstudent{CHARNAME[8];intage;CHARsex[4];CHARdepart[20];floatgrade1,grade2,grade3;}a;floatwage;printf(\nNAME:);scanf(%s,a.NAME);printf(Sex:);scanf(%s,a.sex);printf(Dept:);scanf(%s,a.depart);printf(Grade1:);scanf(%f,&a.grade1);printf(Grade2:);scanf(%f,&a.grade2);printf(Grade3:);scanf(%f,&a.grade3);wage=a.grade1+a.grade2+a.grade3;printf(Thesumofwageis%6.2f\n,wage);}作业2:应用以下结构体,编程输出最高分和其对应号码(num)structstu{intnum;CHARsex;floatscore;}pupil[5]={{101,'M',45.8},{102,'M',62.5},{103,'F',92.5},{104,'F',87.6},{105,'M',58.8}};structstu{intnum;charsex;floatscore;}pupil[5]={{101,M,45.8},{102,“M,62.5},{103,M,52.5},{104,M,72.5},{105,M,42.5},};voidavg(structstu*p){inttnum,i;floattscore;tscore=p-score;tnum=p-num;p++;for(i=2;i=5;i++,p++)if(tscorep-score){tscore=p-score;tnum=p-num;}printf(themaxscore=%f,thenum=%d\n,tscore,tnum);}voidmain(){avg(pupil);}