structStudent{ char *name; int id; int high; }stu;//其中stu为结构体变量,Student为结构体名字,这个结构体名字可以省略,于是就有第二种定义法
struct { char *name; int id; int high; }stu;
//也可以不省去结构体名称,在接下来的步骤中去定义变量 structStudent{ char *name; int id; int high; }; structStudentstu;
typedef
typedef可以重新定义结构体类型,简化定义变量时的步骤
1 2 3 4 5 6
typedefstructStudent{ char *name; int id; int high; }Student;//将struct Student类重新命名为Student Student stu;//通过Student直接定义新的结构体变量
结构体嵌套
结构体是一种支持多层嵌套的类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
typedefstructBirthday{ int year; int month; int day; }Birthday; typedefstructStudent{ char *name; int id; int high; Birthday birthday;//将结构体Birthday嵌套进Student中 }Student; Student stu; //访问 printf("student name:%s student birthday:%d-%d-%d",stu.name,stu.birthday.year,stu.birthday.month,stu.birthday.day);