第 10讲 结构体一,结构体类型二,结构体变量三,结构体变量的引用四、结构体变量的初始化五、结构体数组结构体类型的数据由数目固定,类型不同的若干成员构成,在内存中占有一段连续的存储空间。不同的结构体类型其成员不同。
在程序中使用结构体,必须先做两项工作:
1)定义结构体类型:描述该结构体的成员名称、
成员类型。
2)定义结构体类型变量:根据结构体类型位为定义的结构体变量分配存储空间。
一,结构体类型结构体类型声明的格式为:
struct 结构体名
{ 类型名 1 结构成员名 1 ;
类型名 2 结构成员名 2 ;
···
类型名 n 结构成员名 n ;
} ;
结构体类型声明的格式说明,
struct,关键字,是定义结构体类型的标志。
结构体名:用户定义的 结构体类型名,其命名规则同变量名。
类型名 1~ n,说明了结构成员的类型。
结构成员名 1~ n,用户定义的一个或多个结构体成员的名称,其命名规则同变量名。多个同类型的成员彼此间用逗号分隔。
定义举例
(1) struct date
{ int year,month,day ; } ;
(2) struct student
{ char name[8];
int score[2],burse ;
int year,month,day ;
} ;
二,结构体变量结构体类型的变量的定义方式:
( 1)先声明结构体类型,再定义结构体类型的变量。
( 2)在声明结构体类型的同时定义结构体类型的变量。
( 3)直接定义结构体类型的变量。
方式( 1)
若事先已声明了结构体类型,那么直接用下面格式定义结构体变量。
struct 结构体名 结构体变量名;
例,struct date yesterday ;
存储形式,
yesterday daymonthyear
2字节 2字节 2字节方式( 2)
在声明结构体类型的同时定义结构体类型的变量 。 定义格式为,
struct 结构体名
{ 类型名 1 结构成员名 1 ;
类型名 2 结构成员名 2 ;
···
类型名 n 结构成员名 n ;
} 结构体变量名表 ;
例,struct student
{ char name[8];
int score[2],burse ;
int year,month,day ;
} tongxue1,gongxue2;
存储形式:
name[8] score[0] score[1] burse year month day
8字节 2字节 2字节 2字节 2字节 2字节 2字节方式( 3)
在声明结构体类型时可以直接定义结构体变量,其定义格式为:
struct
{ 类型名 1 结构成员名 1 ;
类型名 2 结构成员名 2 ;
···
类型名 n 结构成员名 n ;
} 结构体变量名表 ;
例,struct birthday { int year,month,day ; } ;
struct
{ char name[8];
int score[2],burse ;
struct birthday csny ;
} tongxue1,tongxue2;
存储形式:
name[8] score[0] score[1] burse brithdayyear month day
8字节 2字节 2字节 2字节 2字节 2字节 2字节关于结构体的说明:
类型与变量是不同的概念,注意区分 。
系统可以对变量赋值,存取,运算,而类型则不能 。
编译时,系统只对变量分配存贮空间,而类型则不分配
结构体中的成员 (即 "域 ")也可以是一个结构体变量,即结构体的嵌套 。
结构体中的成员名可以和程序中的变量同名;不同结构体中的成员名也可以同名 。
三,结构体变量的引用结构体变量的引用方式为:
结构体变量名,成员名其中
".":为结构体成员运算符,1级运算符 。
结构体变量的引用规则:
例,struct student
{ char name[8];
int score[2],burse;
struct birthday csny ;} t1,t2;
不能将结构体变量作为一个整体进行输入和输出。
scanf("%s%d%d%d%d%d%d",&t1.name,&t1.score[0],
&t1.score[1],&t1.burse,&t1.csny.year,
&t1.csny.month,&t1.csny.day);
printf("%s,%d,%d,%d,%d,%d,%d\n",t2.name,
t2.score[0],t2.score[1],t2.burse,
t2.csny.year,t2.csny.month,t2.csny.day);
内嵌结构体成员的引用,必须逐层使用成员名定位,找到最底层的成员。
例 t1.burse
t2.csny.year。
若结构体中的成员是字符型数组时,则可将其看作是“字符串变量”,而直接引用。
例 t1.name是对成员字符数组 name的整体引用。
若结构体中的成员是数值型数组时,则对该数组成员的引用,应为对该数组元素的引用。
例 t1.score[0]
t1.score[1]
四、结构体变量的初始化格式:
struct 结构体名 结构体变量名 ={初始化表 };
特点:
对结构体变量进行初始化时,系统是按每个成员在结构体中的顺序一一对应赋初值的。
例,struct student
{char name[8];
int score[2],burse;
struct birthday csny;
} tongxue1={“张三”,98,95,1000,1983,5,23};
存储形式:
name[8] score[0] score[1] burse brithdayyear month day
张三 98 95 1000 1983 5 23
例,结构体变量的输入与输出。
struct birthday
{ int year,month,day ; } ;
struct student
{ char name[8];
int score[2],burse ;
struct birthday csny ; };
main( )
{ struct student t1,t2;
scanf("%s%d%d%d%d%d%d",t1.name,&t1.score[0],&t1.score[1],
&t1.burse,&t1.csny.year,&t1.csny.month,&t1.csny.day);
t2=t1;
printf("%s,%d,%d,%d,%d,%d,%d\n",t2.name,t2.score[0],
t2.score[1],t2.burse,t2.csny.year,t2.csny.month,t2.csny.day );
}
五、结构体数组格式:
struct 结构体名 结构体数组名 ={初始化表 };
struct [ 结构体名 ]
{ 类型名 1 结构成员名 1;
类型名 2 结构成员名 2;
···
类型名 n 结构成员名 n ;
} 结构体数组名表 = {初始化表 } ;
1、定义定义结构体数组的方法和定义结构体变量的方法一样,只是必须说明其为数组。
定义结构体变量的三种方法都可以用来定义结构体数组。
( 1)先定义结构体类型,再定义变量。
struct person
{ char name[20];
char sex;
int age;
float height;
} ;
struct person per[3];
( 2)定义类型的同时定义变量。
struct person
{ char name[20];
char sex;
int age;
float height;
} per[3];
( 3)直接定义结构体类型变量,省略类型名。
struct
{ char name[20];
char sex;
int age;
float height;
} per[3];
2、初始化和一般数组一样,结构体数组也可以进行初始化。
数组每个元素的初值都放在一对大括号中,
括号中依次排列元素各成员的初始值。
例,struct person
{ char name[20];
char sex;
int age;
float height;
} per[3] = {{“Li ping”,’M’,20,175.5},
{“Wang ling”,’F’,19,162.5}
{“zhaohui”,’M’,20,176}};
例题,结构体数组的引用
struct birthday
{ int year,month,day ; } ;
struct student
{ char name[8];
int score[2],burse ;
struct birthday csny ; };
main( )
{ struct student t[30];
int i;
for (i=0 ; i<30 ; i++ )
scanf("%s%d%d%d%d%d%d",t[i].name,&t[i].score[0],
&t[i].score[1],t[i].burse,&t[i].csny.year,&t[i].csny.month,&t[i].csny.day);
for (i=0 ; i<30 ; i++ )
printf("%s,%d,%d,%d,%d,%d,%d\n",t[i].name,t[i].score[0],
t[i].score[1],t[i].burse,t[i].csny.year,t[i].csny.month,t[i].csny.day);
}
例题,统计学生人数,并计算平均成绩
#define N 6
main( )
{ struct student
{ char name[20];
char sex;
int score;
};
struct student stu[N];
int i,m_num=0,f_num=0;
float average,sum=0;
for(i=0;i<N;i++) { printf(“Please input name,”); \*输入学生信息 *\
gets(stu[i].name); \*输入学生成绩 *\
printf(" sex,");
scanf("%c",&stu[i].sex);
printf(" score:");
scanf("%d",&stu[i].score);
getchar(); }
for(i=0;i<N;i++) \*计算学生人数和总成绩 *\
{ if (stu[i].sex=='M'||stu[i].sex=='m')
m_num=m_num+1;
else f_num=f_num+1;
sum=sum+stu[i].score;
}
average=sum/N; \*计算平均成绩并输出 *\
printf("m_num=%d,f_num=%d,average=%7.2f\n",
m_num,f_num,average);
}