结构体:
有时需要将不同类型的数据组合成一个人有机的整体,以便于引用。
如:一个学生有学号/姓名/性别/年龄/地址等属性 int num; char name[20]; char sex; int age;char addr[30]。
定义一个结构体的一般形式为:
struct 结构名
{
成员列表(其形式为:类型说明符 成员名; 由若干个成员组成)
};
例如:
struct student
{
int num;
char name[20];
char sex;
int age;
char addr[30]
};
定义结构体变量的方法:
1、先声明结构体类型在定义变量名。
例如:struct student student1,student2;
类型名 结构体 变量名 变量名
定义了student1和student2为struct student类型的变量,即它们具有struct student类型的结构。
在定义了结构体变量后,,系统会为之分配内存单元。student1和student2在内存中各占(4+20+1+4+30=63)个字节。
2、在声明类型的同时定义变量。
struct (结构体名)
{
成员表列
}变量名表列;
下面有这么一个表列,并定义两个结构体变量,应该怎么定义?
首先定义一个结构date,由month,day,year三个成员组成。在定义并说明变量boy1和boy2时,其中的成员birthday被说