c语言 12-2

1、

#include <stdio.h>

#define NAME_LEN 64
typedef struct student{   // 为类型声明 typedef名,  Student相当于 struct student 
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;
} Student;

void con(Student *x)  // 函数的形参为struct student型结构体指针 
{
    if(x -> height < 180)  // 使用箭头运算符 访问结构体的成员 
        x -> height = 180;
    if(x -> weight > 80)
        x -> weight = 80;
}
int main(void)
{
    Student sanata;
    
    printf("sanata.name = "); scanf("%s", sanata.name);
    printf("sanata.height = "); scanf("%d", &sanata.height);
    printf("sanata.weight = "); scanf("%f", &sanata.weight);
    printf("sanata.schols = "); scanf("%ld", &sanata.schols);
    
    con(&sanata);  // 调用函数, 实参为结构体的指针 
    
    puts("\n==============================");
    printf("sanata.name = %s\n", sanata.name);
    printf("sanata.heght = %d\n", sanata.height);
    printf("sanata.weight = %.1f\n", sanata.weight);
    printf("sanata.schols = %ld\n", sanata.schols);
    
    return 0;
}

 

posted @ 2021-06-04 10:27  小鲨鱼2018  阅读(51)  评论(0编辑  收藏  举报