结构

来源:互联网 发布:nginx源码 编辑:程序博客网 时间:2024/04/30 03:41
#include<stdio.h>#include<string.h>struct student{    int num;                            //学号    char name[8];                       //姓名    float score;                        //成绩};//根据学生姓名返回学生信息struct student search(struct student *s,char *name){   struct student temp;   temp.num=0;                          //若查找失败,学号置0.   strcpy(temp.name,"null");            //若查找失败,姓名置null.   temp.score=0.0;                      //若查找失败,成绩置0.0   int i;   for(i=0;i<3;i++)   {       if(strcmp(name,s[i].name)==0)    //若查找成功,重置学生信息。        temp=s[i];   }   return temp;                         //返回学生信息}main(){    //对结构体数组初始化    struct student stu[3]={{1001,"张三",98},{1002,"李四",85},{1003,"王五",74}};    struct student h;    char name[8];    printf("请输入查找的姓名:");    scanf("%s",name);    h=search(stu,name);    printf("num=%d,name=%s,score=%.1f",h.num,h.name,h.score);}