c语言中为结构体类型名定义typedef名。

typedef的作用是对数据类型进行同义声明。

1、

#include <stdio.h>

#define NAME_LEN 64

typedef struct student{  //结构的类型名是struct student, 此处使用typedef为类型名strucnt student声明了Student的 typedef名,以下可以使用Student来代替 struct student。 
    char name[NAME_LEN];  //当为结构体类型定义 typedef名时, 结构名可以省略,本利中结构名是student,也就是说student可以省略。 
    int height;
    float weight;
    long schols; 
}Student;

void fun(Student *x)  // 函数的形参为指向Student型的结构体对象的指针 ,此处使用结构体类型的 typedef名代替结构体类型名 
{
    if(x -> height < 180) // 使用箭头运算符可以访问结构体对象指针指向的结构体成员,因为传入的是指针,因此可以对结构体对象的结构体成员进行修改 
        x -> height = 180;
    if(x -> weight > 80)
        x -> weight = 80;
}

int main(void)
{
    Student sanata = {"Sanata", 173, 87.2, 80000};  //此处使用typedef名代替结构体类型名。 
    
    fun(&sanata);  // 函数的实参是 结构体对象的指针, 使用取址运算符获取地址(指针) 
    
    printf("sanata.name:  %s
", sanata.name);
    printf("sanata.height:  %d
", sanata.height);
    printf("sanata.weight:  %.2f
", sanata.weight);
    printf("sanata.schols:  %ld
", sanata.schols);
    
    return 0;
}

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14852179.html