结构体指针---恶补之六

来源:互联网 发布:河南昭大网络靠谱吗 编辑:程序博客网 时间:2024/05/18 14:43

指向结构体对象的指针变量既可指向结构体变量,也可指向结构体数组中的元素,指针变量的基类型必须与结构体变量的类型相同。
struct Student *pt;

如果p指向一个结构体变量stu,以下3种方法等价:
1、stu.成员名(如stu.num);
2、(*p).成员名(如( *p).num);
3、p->成员名(如p->num;)

#include<stdio.h>#define N 3//if 3 peoplestruct Student{    int num;    char name[20];    float score[3];    float aver;};int main(){    void input(struct Student stu[]);    void print(struct Student stu);    struct Student max(struct Student stu[]);    struct Student stu[N],*p=stu;    input(p);    print(max(p));    return 0;}void input(struct Student stu[]){    int i;    printf("please input some information:");    for(i=0;i<N;i++)    {        scanf("%d,%s,%f,%f,%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);        stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;    }}void print(struct Student stud){    printf("number:%d\nname:%s\nthree courses grades:%5.1f,%5.1f,%5.1f\naverage:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);}struct Student max(struct Student stu[]){    int i,m=0;    for(i=1;i<N;i++)    {        if(stu(i).aver>stu(m).aver)        m=i;    }    return stu[m];}
1 0
原创粉丝点击