http://www.qhnu.edu.cn/depart/ncre/cprogram/frame/cd01.htm
方法一:在定义结构体类型的同时,直接给出结构体变量。
如: struct 结构体名
{ 结构体成员变量的定义;
} 变量名1,变量名2,...,变量名n;
方法二:先给出结构体类型的定义,再定义结构体变量。
如: struct 结构体名
{ 结构体成员变量的定义; 例:
} ;
struct 结构体名 变量名1,变量名2,...,变量名n;
方法一: struct student { int number; char name[20]; char sex; int age; float score; char addr[30]; }student1,student2; | 方法二: struct student { int number; char name[20]; char sex; int age; float score; char addr[30]; }; struct studentstudent1,student2; |
请注意两种方法的不同特点:
(1)“;”的用法;
(2)方法二可以用一个*.h文件来存储结构体的定义。
4、要点:
结构体名(struct):
用于标识一种新的数据类型,即结构体类型。
注意区分结构体类型与基本数据类型的不同,它是复合数据类型。
结构体成员变量与普通变量的定义一样,它还可以是结构体变量。
struct date
{int month,day,year;};
struct student
{ int num;
char name[20];
char sex;
int age;
struct date birthday;
char addr[30];
}student1,student2;