结构体
结构体是一种由我们定义的数据类型
定义语法
struct+结构体名
{
成员列表
} ;
注意:1.名字由关键字struct和结构体名组合而成
2.a.结构体普通变量通过"."来访问成员
例如:Tom.num
b.结构体指针变量通过“->”来访问成员,
例如:p->id
1.先声明,后定义
struct students{
int num;
int C_score;
int ps_score;
float avg;
};
int main() {
struct students tom;//tom是结构体变量名
//student是结构体名
return 0;
}
使用:结构体中变量赋初值
1.struct students tom = {10,90,98,94.5};
struct students num[3] = .....
2.tom.num = 10;
tom.C_score = 95;
......
2.在声明变量的同时,定义变量
struct students{
int num;
int C_score;
int ps_score;
float avg;
}tom; //}nums[3];
3.省略结构体名,直接定义结构体变量
struct {
int num;
int C_score;
int ps_score;
float avg;
}tom; //}nums[3];
struct students{
int num;
int C_score[3];
float avg;
};
int main() {
struct students tom;//tom是结构体变量名
//student是结构体名
struct students tom{10,{90,80,70},85.5};
struct students tom{10,90,80,70,85.5};
printf("序号:%d",tom.num);
......
return 0;
}
结构体数组
struct 结构体名 {
数据类型 成员变量名
......
};
语法: struct 结构体名 数组名[数组长度]
struct student class2103[6]
main函数中 struct 数组名 [数组长度] = {初值列表}
{10,20,30},{40,50,60}......
typedef的使用(type define类型定义)
注意:他不是用来创造类型 ,而是给已有的类型取一个别名
如何知道那个是类型
int a;
int a[10];
struct student stu;
规则:将名字去掉,剩下就是我类型
int
int[10]
struct student
语法:typedef 已有的类型 别名;
例如:typedef int TYPE
//TYPE与int等价,如TYPE a;相当于int a;
typedef struct student {
int num;
int age;
}STU; //STU是 struct student的别名