结构体初始化 访问的三种方式(结构体指针)


输出三个学生中成绩最高的学生信息

1> 

#include
typedef struct student
{
    int num;
    char name[10];
    int score;


} Student;
int main()
{
    Student s1,s2,s3,max;
    scanf("%d %s %d",&s1.num,s1.name,&s1.score);
    scanf("%d %s %d",&s2.num,s2.name,&s2.score);
    scanf("%d %s %d",&s3.num,s3.name,&s3.score);
    max=s1;
    if(max.score     if(max.score     printf("%d %s %d",max.num,max.name,max.score);
}


2> 结构体指针;

#include
typedef struct student
{
    int num;
    char name[10];
    int score;


} Student;
int main()
{
    Student s1,s2,s3,*p;//*p,结构体指针
    scanf("%d %s %d",&s1.num,s1.name,&s1.score);
    scanf("%d %s %d",&s2.num,s2.name,&s2.score);
    scanf("%d %s %d",&s3.num,s3.name,&s3.score);
    p=&s1;
    if(p->score
    if((*p).score//p->和(*p).相同;
    printf("%d %s %d",p->num,p->name,(*p).score);
}

访问成员变量三种方式:s1.score,p->score,(*p).score;

结构体初始化

student s1={1001,lingming,98};

*p=NULL;

你可能感兴趣的:(C语言)