[KungFu8位 MCU] 结构体定义的4种方法

[复制链接]
 楼主| piteqiu 发表于 2019-7-30 16:03 | 显示全部楼层 |阅读模式
1. 先定义结构体类型,再定义结构体变量
struct student{

    char no[20];       //学号

    char name[20];    //姓名

     char sex[5];    //性别

    int age;          //年龄

};            

struct student stu1,stu2;

//此时stu1,stu2为student结构体变量

 楼主| piteqiu 发表于 2019-7-30 16:04 | 显示全部楼层
2. 定义结构体类型的同时定义结构体变量。
struct student{

    char no[20];        //学号

    char name[20];     //姓名

    char sex[5];      //性别

    int age;            //年龄

} stu1,stu2;

此时还可以继续定义student结构体变量,如:
struct student stu3;
 楼主| piteqiu 发表于 2019-7-30 16:05 | 显示全部楼层
3、不指定类型名而直接定义结构体变量
struct{

    char no[20];        //学号

    char name[20];      //姓名

    char sex[5];      //性别

    int age;          //年龄

} stu1,stu2;



一般不使用这种方法,因为直接定义结构体变量stu1、stu2之后,就不能再继续定义该类型的变量。
 楼主| piteqiu 发表于 2019-7-30 16:06 | 显示全部楼层
4、用typedef定义结构体变量
typedef struct stdudent

  

{

       char name[20];

       int age;

}student_t;


上面的代码,定义了一个结构体变量类型,这个类型有2个名字:第一个名字是struct student;第二个类型名字是student_t.
定义了这个之后,下面有2中方法可以定义结构体变量
第一种: struct student student_1; //定义了一个student_1的结构体变量
第二种:student_t student_1 //定义了一个student_1的结构体变量

jerow 发表于 2019-7-31 15:27 | 显示全部楼层
居然有四种方法,学习了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

292

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

39

主题

292

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部