1.结构体
①什么是结构体?
答:结构体是一种可以将不同类型打包在一起的一种用户自定义类型。
②结构体怎样定义?
答:用关键字struct 来定义
例如:描述一个学生
struct Student {
char name[32]; //姓名
int age; //学号
char sex[5]; //性别
char id[20]; //学号
}stu;
注意:
1.再次定义变量时时可采用以下形式进行二次定义
struct Student a;
2.也可用以下形式定义
typedef struct Student {
char name[32]; //姓名
int age; //学号
char sex[5]; //性别
char id[20]; //学号
}stu;
此时再次调用可采取这种形式定义,如:
stu a;
③结构体的访问
结构体的访问有两种方法
stu.age=22;//将22赋值给stu中的age
stu->age=22;
④结构体的自引用
结构体可以在其内部成员内引用自己,如:
struct Student {
int data;
struct Student *next;
};
这样在访问结构体内部成员的next时 ,可通过它来访问到下一个相同类型的结构体。