------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
构造类型 结构体、枚举和关键字typedef
构造类型 由多个或者已经定义的类型元素用构造的方法,构造新的类型
构造类型:数组 结构体
一、结构体
1.结构体概念:
由相同类型或者不同类型的数据用构造方法,构造的类型
结构体的定义
struct 结构体名{
成员列表
}
定义一个汽车结构体
struct Car{
char*color;
int lunzi;
int speed;
};
定义结构体三种方法
1)先定义结构体,然后在定义结构体变量
struct Student{
//学生学号
int sno ;
//学生姓名
char [10];
//学生年龄
int age;
//学生成绩
float score;
};
注意:
结构体定义完以后,计算机并不会给结构体分配内存空间,会在定义结构体变量后分配变量
结构体变量定义格式 struct 结构体 结构体变量名;
struct Student stu1; 这句话表示定义一个Student结构体变量,变量名为 stu1
//stu1 是Student类型,stu1可以存放学生的信息。成绩,姓名...
struct Student stu1,stu2,stu3;可以定义多个结构体变量
2)定一结构体的同时,定义结构体变量
格式
struct 结构体名{
}结构体变量1,结构体变量2。。。;
struct Student{
//学生学号
int sno ;
//学生姓名
char [10];
//学生年龄
int age;
//学生成绩
float score;
}stu3,stu4,....;
3)匿名结构体定义结构体变量
struct {
}结构体变量1,结构体变量2,...;
struct {
int lunzi;
int speed;
char*color;
}car1,car2,car3;
2结构体变量中成员访问方法
结构体变量名.成员变量
//定义一个Car类型的结构体
struct Car{
char*pp;
int lunzi;
int speed;
};
//用结构体类型,定义结构体变量
struct Car car1;
用 car1.pp ;
car1.lunzi;
car1.speed;
3结构体变量的初始化
struct Student{
//学号
int sno;
//学生姓名
char name[21];