1、概述 C 语言允许用户自己指定这样一种数据结构,它由不同类型的数据组合成一个整体,以便引用,这些组合在一个整体中的数据是互相联系的,这样的数据结构称为结构体,它相当于其它高级语言中记录。
声明一个结构休类型的一般形式如下:
struct 结构体名{ 成员列表} ;
结构体名,用作结构体类型的标志,它又称 结构体标记,大括号内是该结构体中的各个成员,由它们组成一个结构体,对各成员都应进行类型声明如:
类型名 成员名;
也可以成员列表称为域表,第一个成员也称为结构体中的一个域。成员名定名规则写变量名同。
struct student { int num; char name[ 20 ] ; char sex; int age; float score; char addr[ 30 ] ; } ;
2、定义结构体类型变量的方法 前面只是指定了一个结构体类型,它相当于一个模型,但其中并无具体数据,系统对之也不分配实际内存单元,为了能在程序中使用结构类型的数据,应当定义结构体类型的变量,并在其中存放具体的数据,可以采取以下3种方法定义结构体类型变量。
(1)先声明结构体类型再定义变量名
如上面已定义了一个结构体类型 struct student,可以用它来定义变量。如:
struct student { . . . . . . . . . } student1, student2
定义了 student1, student2 为 struct student 类型的变量。
在定义了结构体变量后,系统会为之分配内存单元。例如 student1 和 student2 在内存中各占 59 个字节。
应当注意,将一个变量定义为标准类型(基本数据类型)与定义为结构体类型不同之处在于后者不仅要求指定变量为结构体类型,而且要求指定为某一特定的结构体类型(例如 struct student 类型),因为可以定义出许多种具体的结构体类型。而在定义变量为整形时,只需指定为 int 型即可。
(2)在声明类型的同时定义变量
例如:
struct student { int num; char name[ 20 ] ; char sex; int age; float score; char addr[ 30 ] ; } student1, student2;
它的作用与第一种方法相同,即定义了两个 struct student 类型的变量 student1, student2 这种形式的定义的一般形式为
struct 结构体名{ 成员表列} 变量名表列;
(3)直接定义结构类型变量
其一般形式为:
struct { 成员表列} 变量名表列;
即不出现结构体名。
关于结构体类型,有几点要说明:
a. 类型与变量是不同的概念,不是混同,只能对变量赋值,存取或运算,而不能对一个类型赋值,存取或运算。在编译时,对类型是不分配空间的,只对变量分配空间。
b. 对结构体中的成员(即 域)可以单元使用,它的作用与地位相当于普通变量。
c. 成员也可以是一个结构体变量。如:
struct date { int month; int day; int year; } struct student { int num; char name[ 20 ] ; char sex; int age; struct date birthday; char addr[ 30 ] ; } student1, student2;
先声明一个 struct date 类型,它代表 日期 包括3个成员 month, day, year。然后在声明 struct student 类型时,将成员 birthday 指定为 struct date 类型。
d. 成员名可以与程序中的变量名相同,二者不代表同一对象。
3、结构体变量的引用 (1)不能将一个结构体变量作为一个整体进行输入和输出。
只能对结构体变量中的各个成员分别进行输入输出。引用结构体变量中的成员的方式为:
结构体变量名. 成员名
例如 student1.num 表示 student1 变量中的 num 成员,即 student1 的 num 项,可以对变量的成员赋值。例如: student1.num = 10010;
. 是成员(分量)运算符,它在所有的运算符中优先级最高,因此可以把 student1.num 作为一个整体来看待。上面的赋值语句作用是将整数 10010赋给 student1 变量中的成员 num。
(2)如果成员本身又属一个结构体类型,则要用若干个成员运算符,一级一级地找到最低一级的成员。只能对最低的成员进行赋值或存取以及运算。
例如:结构体变量 student1 可以这样访问各成员:
student1. num student1. birthday. month
注意,不能用 student1.birthday 来访问 student1 变量中的成员 birthday,因为 birthday 本身是一个结构体变量。
(3)对结构体变量的成员可以像普通变量一样进行各种运算(根据其类型决定可以进行的运算)。
student2. score = student1. score; sum = student1. score + student2. score; student1. age ++ ; ++ student1. age;
由于 . 运算符的优先级最高,因此 student1.age ++ 是对 student1.age 进行自加运算。而不是先对 age 进行自加运算。
(4)可以引用结构体变量成员的地址。也可以引用结构体变量的地址。如:
scanf ( "%d" , & student1. num) ; printf ( "%o" , & student1) ;
但不能用以下语句整体读入结构体变量如:
scanf ( "%d,%s,%c,%d,%f,%s" , & student1) ;
结构体变量的地址主要用于作函数参数,传递结构体的地址。
4、结构体变量的初始化 和其它类型变量一样,对结构体变量可以在定义时指定初始值。
实例
typedef struct mystruct { const char * name; long int age; float score; } my; typedef struct teacher { int name; my student; const char * ping; } tec; tec system; void zero ( mystruct t) { t. name = "张信哲" ; t. age = 18 ; t. score = 98.2 ; } void one ( mystruct t[ ] ) { t[ 0 ] . name = "张三" ; t[ 0 ] . age = 20 ; t[ 0 ] . score = 99.9 ; t[ 1 ] . name = "张飞" ; t[ 1 ] . age = 88 ; t[ 1 ] . score = 99.111 ; } void two ( tec system) { system. name = 01 ; system. student; system. ping = "A+" ; }
5、结构体数组 一个结构体变量中可以存放一组数据(如一个学生的学号,姓名,成绩等数据)。如果有10个学生的数据需要参加运算,显然应该用数组,这就是结构体数组。结构体数组与以前介绍过的数据值型数组不同之处在于每个数组元素都一个结构体类型的数据,它们分别包括各个成员(分量)项。
(1) 定义结构体数组
和定义结构体变量的方法相仿,只需说明其为数组即可。
struct student { int num; char name[ 20 ] ; char sex; int age; float score; char addr[ 30 ] ; } ; struct student stu[ 3 ] ;
以上定义了一个数组 stu,其元素为 struct student 类型数据,数组有 3 个元素。也可以直接定义一个结构体数组。如:
struct student { int num; . . . . } stu[ 3 ] ; 或struct { int num; . . . } stu[ 3 ] ;
(2) 结构体数组的初始化
与其它类型数组一样,对结构体数组可以初始化如:
struct student { int mum; char name[ 20 ] ; char sex; int age; float score; char addr[ 30 ] ; } stu[ 3 ] = { { 10101 , "Li Lin" , 'M' , 18 , 87.5 , "103 Beijing Road" } , { 10101 , "Li Lin" , 'M' , 18 , 87.5 , "103 Beijing Road" } , { 10101 , "Li Lin" , 'M' , 18 , 87.5 , "103 Beijing Road" } } ;
定义数组 stu 时,元素个数可以不指定,即写成以下形式:
stu[ ] = { { . . . } , { . . . } , { . . . } } ;
编译时,系统会根据给出初值的结构体常量的个数来确定数组元素的个数。
当然,数组的初始化也可以用以下形式:
struct student { int num; . . . } ; struct student stu[ ] = { { . . . } , { . . . } , { . . . } } ;
即先声明结构体类型,然后定义数组为该结构体类型,在定义数组时初始化。