目录
一、定义和引用结构体变量
1、怎样创建结构体类型(相当于建房子时的图纸)
2、怎样定义结构体类型变量
3、引用结构体变量
二、使用结构体数组
1、定义结构体数组
2、typedef
三、结构体指针
1、指向结构体变量(或数组)的指针
2、用结构体变量和结构体变量的指针作函数参数
写在前面:C语言提供了一些由系统已建立好的标准数据类型,如:int float等。但人们在处理问题时,只用系统所提供的完全不够,因而,C语言允许用户根据需要自行创建一些数据类型,并用它来定义变量。
一、定义和引用结构体变量
1、怎样创建结构体类型(相当于建房子时的图纸)
结构体:用户自己建的由不同类型数据组成的组合型数据结构
注:struct Student —为类型名(相当于int char等) (struct 是声明结构体类型时所必须的关键字)
声明一个结构体类型的一般形式为:
struct 结构体名
{成员列表};
特别注意:分号不可少
成员列表:类型名 成员名;
结构体名首字母一般大写,这是习惯
struct Student//结构体类型
{
int num;//学号
char name[20];//姓名
char sex;//性别
int age;//年龄
float score;//成绩
char addr[30];//家庭地址
};
//一个结构体中的成员类型可以是另一个结构体类型
struct Date{
int month;
int day;
int year;
};
struct Student{
int num;
char name[20];
char sex;
int age;
float score;
struct Date birthday;//成员birthday属于struct Date类型
char addr[30];
};
2、怎样定义结构体类型变量
a.先声明结构体类型,再定义该类型的变量
struct Student stu1,stu2;
类型名 变量名
相当于int a,b;
b.在声明类型的同时定义变量
struct 结构体名
{
成员列表
}变量名列表;
struct Student{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
}stu1,stu2;
c.不指定类型名而直接定义结构体类型变量(不常用)
struct
{
成员列表
}变量名列表;
3、引用结构体变量
a.在定义结构体变量时可初始化
struct Student{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
}a = {12,"黎明",'M',1,97,"diqoucun 110"};//初始化a
b.引用结构体变量中成员的值
结构体变量名.成员名 a.num = 12; 若结构体中含结构体则应为 a.birthday.month
二、使用结构体数组
1、定义结构体数组
定义结构体数组的形式
struct 结构体名{
成员列表
}数组名[数组长度]
注:对结构体数组初始化的形式是在定义数组的后面加上:
= {初值列表}
//题目:3人候选,每个选民只能投一票,共十个选民
//要求:编写一个计票器,先后输入候选人名字,最后统计票数
#include
#include
struct Person{
char name[20];//候选人姓名
int count;//候选人所得票数
}leader[3] = {"A",0,"B",0,"C",0};//定义结构体数组并初始化
int main(){
int i = 0,j = 0;
char leader_name[20];//定义字符数组,存放被选人姓名
for(i=1;i<=10;i++){
printf("第%d票:> ",i);
scanf("%s",leader_name);//输入所选的候选人姓名
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0)
leader[j].count++;
}
printf("投票结果如下:n");
for(i=0;i<3;i++)
printf("%s:%dn",leader[i].name,leader[i].count);
return 0;
}
2、typedef
tupedef为类型重定义(即:将类型名换个名称)
//struct 结构体关键字 Stu结构体标签 struct Stu结构体类型
typedef struct Stu{
//成员变量
char name[20];
int age;
char tele[12];
char sex[5];
}stu;//将类型重定义为stu
void print1(struct Stu a){
printf("%sn",a.name);
printf("%dn",a.age);
printf("%sn",a.tele);
printf("%sn",a.sex);
}
void print2(struct Stu *a){
printf("%sn",a->name);
printf("%dn",a->age);
printf("%sn",a->tele);
printf("%sn",a->sex);
}
int main(){
struct Stu s = {"张三",19,"1247465","男"};
print1(s);
print2(&s);
// s = {"张三",19,"1247465","男"};//此种初始化错误
// stu k;//因为重定义,所以此时可用类型名stu
return 0;
}
三、结构体指针
一个结构体变量的起始地址就是这个结构体变量的指针
1、指向结构体变量(或数组)的指针
#include
#include
struct Student{
long num;
char name[20];
char sex;
float score;
};
变量指针
int main(){
struct Student stu_1;//定义struct Student类型的变量stu_1
struct Student *p;//定义指向struct Student类型数据的指针变量
p = &stu_1;//p指向stu_1
stu_1.num = 1001;//对结构体变量的成员赋值
strcpy(stu_1.name,"A");//用字符串复制函数给stu_1.name赋值
stu_1.sex = 'M';
stu_1.score = 89.5;
puts("学生信息情况如下:");
//三种输出方法(三者等价)
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
(*p).num,(*p).name,(*p).sex,(*p).score);
//*p两侧的括号不能省,因为运算符"."优先于"*"
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
p->num,p->name,p->sex,p->score);
return 0;
}
数组指针
struct Student stu[3]={{1001,"A",'M',88},{1002,"B",'F',99},{1003,"C",'F',100}};
//定义结构体数组并初始化
int main(){
struct Student *p;//定义指向结构图题变量的指针变量
printf("学号 姓名 性别 成绩n");
for(p=stu;pnum,p->name,p->sex,p->score);
}
return 0;
}
2、用结构体变量和结构体变量的指针作函数参数
定义结构体数组的形式
struct 结构体名{
成员列表
}数组名[数组长度]
注:对结构体数组初始化的形式是在定义数组的后面加上:
= {初值列表}
//题目:3人候选,每个选民只能投一票,共十个选民
//要求:编写一个计票器,先后输入候选人名字,最后统计票数
#include
#include
struct Person{
char name[20];//候选人姓名
int count;//候选人所得票数
}leader[3] = {"A",0,"B",0,"C",0};//定义结构体数组并初始化
int main(){
int i = 0,j = 0;
char leader_name[20];//定义字符数组,存放被选人姓名
for(i=1;i<=10;i++){
printf("第%d票:> ",i);
scanf("%s",leader_name);//输入所选的候选人姓名
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0)
leader[j].count++;
}
printf("投票结果如下:n");
for(i=0;i<3;i++)
printf("%s:%dn",leader[i].name,leader[i].count);
return 0;
}
2、typedef
tupedef为类型重定义(即:将类型名换个名称)
//struct 结构体关键字 Stu结构体标签 struct Stu结构体类型
typedef struct Stu{
//成员变量
char name[20];
int age;
char tele[12];
char sex[5];
}stu;//将类型重定义为stu
void print1(struct Stu a){
printf("%sn",a.name);
printf("%dn",a.age);
printf("%sn",a.tele);
printf("%sn",a.sex);
}
void print2(struct Stu *a){
printf("%sn",a->name);
printf("%dn",a->age);
printf("%sn",a->tele);
printf("%sn",a->sex);
}
int main(){
struct Stu s = {"张三",19,"1247465","男"};
print1(s);
print2(&s);
// s = {"张三",19,"1247465","男"};//此种初始化错误
// stu k;//因为重定义,所以此时可用类型名stu
return 0;
}
三、结构体指针
一个结构体变量的起始地址就是这个结构体变量的指针
1、指向结构体变量(或数组)的指针
#include
#include
struct Student{
long num;
char name[20];
char sex;
float score;
};
变量指针
int main(){
struct Student stu_1;//定义struct Student类型的变量stu_1
struct Student *p;//定义指向struct Student类型数据的指针变量
p = &stu_1;//p指向stu_1
stu_1.num = 1001;//对结构体变量的成员赋值
strcpy(stu_1.name,"A");//用字符串复制函数给stu_1.name赋值
stu_1.sex = 'M';
stu_1.score = 89.5;
puts("学生信息情况如下:");
//三种输出方法(三者等价)
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
(*p).num,(*p).name,(*p).sex,(*p).score);
//*p两侧的括号不能省,因为运算符"."优先于"*"
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
p->num,p->name,p->sex,p->score);
return 0;
}
数组指针
struct Student stu[3]={{1001,"A",'M',88},{1002,"B",'F',99},{1003,"C",'F',100}};
//定义结构体数组并初始化
int main(){
struct Student *p;//定义指向结构图题变量的指针变量
printf("学号 姓名 性别 成绩n");
for(p=stu;pnum,p->name,p->sex,p->score);
}
return 0;
}
2、用结构体变量和结构体变量的指针作函数参数
一个结构体变量的起始地址就是这个结构体变量的指针
1、指向结构体变量(或数组)的指针
#include
#include
struct Student{
long num;
char name[20];
char sex;
float score;
};
变量指针
int main(){
struct Student stu_1;//定义struct Student类型的变量stu_1
struct Student *p;//定义指向struct Student类型数据的指针变量
p = &stu_1;//p指向stu_1
stu_1.num = 1001;//对结构体变量的成员赋值
strcpy(stu_1.name,"A");//用字符串复制函数给stu_1.name赋值
stu_1.sex = 'M';
stu_1.score = 89.5;
puts("学生信息情况如下:");
//三种输出方法(三者等价)
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
(*p).num,(*p).name,(*p).sex,(*p).score);
//*p两侧的括号不能省,因为运算符"."优先于"*"
printf("学号:%ldt姓名:%st性别:%ct成绩:%.2fn",
p->num,p->name,p->sex,p->score);
return 0;
}
数组指针
struct Student stu[3]={{1001,"A",'M',88},{1002,"B",'F',99},{1003,"C",'F',100}};
//定义结构体数组并初始化
int main(){
struct Student *p;//定义指向结构图题变量的指针变量
printf("学号 姓名 性别 成绩n");
for(p=stu;pnum,p->name,p->sex,p->score);
}
return 0;
}
2、用结构体变量和结构体变量的指针作函数参数
结构体变量的值传递有3种:a.用结构体变量的成员做参数(即:将实参传给形参)
b.用结构体变量做实参(此种传递在空间和时间上开销较大,少用)
c.用之下那个结构体变量(或数组元素)的指针做实参,将结构体变量(或数组元素)的地址传给形参
以下3个函数的调用情况各不相同 a.input函数——实参(指针p),形参(结构体数组),传地址,无返回值 b.max函数——实参(指针p),形参(结构体数组),传地址,返回结构体类型数据 c.print函数——实参(结构体变量(数组元素)),形参(结构体变量), 传机构体变量中各个成员值,无返回值
题目:录入2名学生的信息 要求:输出平均成绩最高的学生信息 #include#define N 2 //学生人数为2 typedef struct Student{ int num; char name[20]; float score[2];//2门课成绩 float aver; }Stu; //录入信息 void input(Stu stu[]){ int i = 0; printf("录入学生信息:学号 姓名 成绩n"); for(i=0;i stu[m].aver) m=i; return stu[m]; } //输出结果 void print(Stu stud){ puts("成绩最高的学生是:"); printf("学号:%dn姓名:%sn各科成绩:%5.1f %5.1fn平均成绩:%6.2fn", stud.num,stud.name,stud.score[0],stud.score[1],stud.aver); } int main(){ Stu stu[N]; Stu* p = stu; input(p); print(max(p)); return 0; }
不足之处,欢迎留言!!!



