结构体
一、结构体类型与结构体变量的定义
使用结构体表示数据:
1.定义结构体类型
2.定义结构体类型的变量
3.引用结构体变量的各成员变量
相比简单的变量的定义,这一过程需要先自定义结构体类型。
注:
1.结构定义并不预留内存,它只是一个模板
2.结构定义放置在程序的开始部分,位于头文件声明之后。
3结构定义仅描述了一个结构的形式。如果要在程序里使用结构,需要声明结构变量。
定义结构之后,声明变量。
声明变量的形式:
①先定义结构,再声明结构变量:
struct student
{
int num;
char name[20];
float score;
};
struct student
student1,student2;
②在定义结构类型的同时声明结构变量:
struct student
{
int num;
char name[20];
float score;
};
student1,student2;
③直接声明结构变量:
struct
{
int num;
char name[20