C语言基础:结构体

示例代码:

  1. #include            //头文件
  2.                         //结构体,嵌套结构体
  3. struct student{                   //结构体 struct关键字修饰
  4.     int number;                   //学号
  5.     char name[20];            //姓名
  6.     char sex;                      //性别
  7.     
  8.         struct date{              //结构体   日期   嵌套结构体
  9.             int year;                //年
  10.             int month;            //月
  11.             int day;                //日
  12.         }birthday;
  13.     
  14.     float score;                   //总分 
  15.     char speciality[11];      //特长
  16.     char school[31];        //学校
  17. };
  18. main(){
  19.     //结构体变量的初始化
  20.     struct student st1={1001,"Liming",'0',1983,10,25,573,"Computer","3th High School"
  21.     };
  22.     
  23.     printf("%4d %-10s %c %4d %2d %2d %.2f %-10s %s\n",
  24.         st1.number,st1.name,st1.sex,
  25.         st1.birthday.year,st1.birthday.month,st1.birthday.day,
  26.         st1.score,st1.speciality,st1.school
  27.         );
  28.  ---------------------------------------------------------------------------------------------------------------------------  
  29.     //结构体变量的引用   
  30.     struct student st2;
  31.        st2.number=1002;
  32.        strcpy(st2.name,"Zhangsan");
  33.       str2.sex='f';
  34.       str2.birthday.year=1919;
  35.       str2.birthday.month=10;
  36.       str2.birthday.day=1;
  37.       str2.score=666;
  38.       str2.speciality="Car";
  39.       str2.school="1th High School";
  40. -----------------------------------------------------------------------------------------------------------------------------------
  41.      //结构体数组
  42.     int i;  
  43.     struct student st[2]={
  44.         {
  45.             "11","Liming",'0',1983,10,25,573,"Computer","3th High School"
  46.         },
  47.         {
  48.             "12","WangLi",'1',1984,2,16,590,"Physics","Remin Middle School"
  49.         }
  50.     };
  51.     
  52.     for(i=0;i<2;i++) printf("\n%4s %-10s %c %4d %2d %2d %.2f %-10s %s\n",
  53.                             st[i].number,st[i].name,st[i].sex,
  54.                             st[i].birthday.year,st[i].birthday.month,st[i].birthday.day,
  55.                             st[i].score,st[i].speciality,st[i].school
  56.                             );
  57. }
  58. ---------------------------------------------------------------------------------------------------------------
  59. //结构体数组初始化
  60. struct student{
  61.     int num;
  62.     char name[20];
  63.     char sex;
  64.     float score;
  65.     }stu[3]={
  66.         {11,"Liang tao",'t',80},
  67.         {13,"Zhao jun",'t',97.5},
  68.         {14,"Hai kang",'f',58}
  69.     };
  70. ---------------------------------------------------------------------------------------------------------------
  71. //结构体指针
  72. struct student{                               //结构体student
  73.         int num;
  74.         char name[20];
  75.         char sex;
  76.         float score;
  77.     };
  78.     
  79.     struct student stuone;         //结构体变量stuone
  80.     struct student *p;                //结构体指针 *p
  81.     p=&stuone;                         //结构体指针指向结构体变量
  82.     
  83.     stuone.num=10871;            //结构体变量初始化
  84.     strcpy(stuone.name,"Mary");
  85.     stuone.sex='F';
  86.     stuone.score=98.0;
  87.     //直接输出结构体变量的值
  88.     printf("num:%d\nname:%s\nsex:%c\nscore:%.2f\n",
  89.             stuone.num,stuone.name,stuone.sex,stuone.score
  90.         );
  91.     //通过结构体指针输出结构体变量的值 方法1
  92.     printf("num:%d\nname:%s\nsex:%c\nscore:%.2f\n",
  93.             (*p).num,(*p).name,(*p).sex,(*p).score
  94.         );
  95.     //通过结构体指针输出结构体变量的值 方法2                                                 
  96.     printf("num:%d\nname:%s\nsex:%c\nscore:%.2f\n",
  97.             p->num,p->name,p->sex,p->score
  98.         );    
  99. -------------------------------------------------------------------------------------
  100. //结构体变量指针作为参数
  101. struct student{
  102.     int num;
  103.     char name[20];
  104.     int score[3];
  105.     }stu={10020,"Mary",89,98,79};       //结构体变量stu,初始化结构体变量
  106. void print(struct student *p){                //print()函数,传入结构体变量的指针
  107.     printf("%d %s %d %d %d",p->num,p->name,p->score[0],p->score[1],p->score[2]);//输出指针所指信息
  108.     printf("\n");
  109. }
  110. main(){
  111.     print(&stu);         //调用print()函数
  112. }
  113. --------------------------------------------------------------------------------------------
  114. //结构体型的函数
  115. struct person{
  116.         int num;
  117.         char name[20];
  118.         char telephone[12];
  119.         char email[40];
  120.         char addr[30];
  121.     }con_person[3]={                //结构体数组
  122.         {9,"Zhangli","13817890789","[email protected]","Shanghai"},
  123.         {6,"Jack","01089789091","[email protected]","Beijing"},
  124.         {12,"Qiuzhi","13762457986","[email protected]","Hangzhou"}                
  125.         };
  126.         
  127. struct person find(int n){                //结构体函数find()
  128.     int i;
  129.     for(i=0;con_person[i].num!=0;i++){
  130.         if(con_person[i].num==n)
  131.              return(con_person[i]);
  132.     }
  133. }
  134. main(){
  135.     int number;
  136.     struct person result;                //结构体变量
  137.     printf("Enter the number:");
  138.     scanf("%d",&number);          //输入编号
  139.     result=find(number);              //调用结构体函数find()
  140.     if(result.num!=0){
  141.         printf("Name:%s\n",result.name);
  142.         printf("Telephone:%s\n",result.telephone);
  143.         printf("Email:%s\n",result.email);
  144.         printf("Address:%s\n",result.addr);
  145.     }else{
  146.         printf("Not foutd!\n");
  147.     }
  148. }
  149. ----------------------------------------------------------------------------------------------
  150. //结构体指针函数
  151. struct person *find(int n){        //结构体指针型函数find()
  152.     int i;
  153.     for(i=0;con_person[i].num!=0;i++){
  154.         if(con_person[i].num==n)
  155.              return(&con_person[i]);
  156.     }
  157. }
  158.     
  159. main(){
  160.     int number;
  161.     struct person *result;                            //结构体指针型变量
  162.     printf("Enter the number:");
  163.     scanf("%d",&number);                         //输入编号
  164.     result=find(number);                             //调用结构体指针型函数*find()
  165.     if(result->num!=0){
  166.         printf("Name:%s\n",result->name);
  167.         printf("Telephone:%s\n",result->telephone);
  168.         printf("Email:%s\n",result->email);
  169.         printf("Address:%s\n",result->addr);
  170.     }else{
  171.         printf("Not found!\n");
  172.     }
  173. }        

    

你可能感兴趣的:(C,菜鸟学习编程之路)