【c学习-8】

/*继承结构体*/ #include // 定义子结构体 struct date{ int year; int month; int day; }; //定义父结构体 struct student{ char name[20]; int age; char sex; struct date birthday; //父结构体包含子结构体 }; //初始化父结构体变量 int main(){ struct student student={"liupeng",18,"man",{2001,01,30}}; //主函数内分别调用子父结构体 printf("name:%s\n",student.name); printf("year:%dmonth:%dday:%d\n",student.birthday.year,student.birthday.month,student.birthday.day); } /*结构体练习*/ #include int main(){ //定义一个结构体并初始成员 struct books{ int money; char bookname[10]; int dates; char names; }; struct books xiaomin={20,"浮生六记",2018,"xm"}; //对结构体成员变量值进行输出 printf("money:%d\nbookname:%s\ndates%d\nnames:%c\n",xiaomin.money, xiaomin.bookname,xiaomin.dates,xiaomin.names ); } /*结构体练习2*/ #include //定义一个结构体 struct student { char name[10]; int age; char sex; }; int main(){ //定义计数器,用于遍历结构体内成员 int i; //初始化结构体成员 struct student stu[3]={ {"liu",18,"man"}, {"zhang",20,"wom"}, {"wu",40,"man"} }; for(i=0;i<3;i++){ printf("name:%s\nage:%d\nsex:%c\n",stu[i].name, stu[i].age,stu[i].sex ); } } /*结构体练习3*/ #include #define N 5 //预定义常量N //定义结构体 struct x{ int num; int cro; }a[10]; //初始化结构体变量 int main(){ //定义两个计数器 int i,j; //循环遍历输入 for(i=0;i =1;i--) for(j=i-1;j>=0;j--) if(a[i].num

猜你喜欢

转载自www.cnblogs.com/activecode/p/9509240.html