一、定义
结构体:不同类型数据的集合,是复合类型,这些数据称为结构体的成员。
1.结构体类型定义
语法格式:
struct [结构体标签]{
成员类型 成员名称;
成员类型 成员名称;
..................................;
成员类型 成员名称;
};
注:结构体标签可省略
例如:
struct student{
int sno;
char name[20];
float score;
};
2.结构体变量的定义
1.格式一:常规格式
struct student{
int sno;
char name[20];
float score;
};
struct student st1;
2.格式二:与类型同时定义
struct student{
int sno;
char name[20];
float score;
}st1;
3.格式三:直接定义
struct {
int sno;
char name[20];
float score;
}st1;
二、结构体变量的初始化
1.完全初始化
struct student{
int sno;
char name[20];
float score;
};
struct student st1 = {1001,"鹿晗",89.87};
2.部分初始化
struct student{
int sno;
char name[20];
float score;
};
struct student st2 = {1002,"蔡徐坤"};
3.指定成员初始化
struct student{
int sno;
char name[20];
float score;
};
struct student st3 = {.name="坤坤",.sno=1003};
三、结构体变量赋值,结构体变量的输出
1.分别给每个成员赋值
struct student{
int sno;
char name[20];
float score;
};
struct student st4;
st4.sno = 1004;
strcpy(st4.name,"关晓彤");
st4.score = 98.89;
2.同类型结构体变量,相互赋值
struct student st5;
struct student st1 = {1001,"鹿晗",89.87};
st5 = st1;//把st1的值赋值给st5
3.取值打印--只能分别打印每一个成员
#include <stdio.h>
struct student{
int sno;
char name[20];
float score;
};
int main(void)
{
//变量初始化
struct student st1 = {1001,"鹿晗",89.87};
struct student st2 = {1002,"蔡徐坤"};
struct student st3 = {.name="坤坤",.sno=1003};
//定义变量
struct student st4;
struct student st5;
//变量赋值
st4.sno = 1004;
strcpy(st4.name,"关晓彤");
st4.score = 98.89;
st5 = st1;
//打印
printf("%d %s %.2f\n",st1.sno,st1.name,st1.score);
printf("%d %s %.2f\n",st2.sno,st2.name,st2.score);
printf("%d %s %.2f\n",st3.sno,st3.name,st3.score);
printf("%d %s %.2f\n",st4.sno,st4.name,st4.score);
printf("%d %s %.2f\n",st5.sno,st5.name,st5.score);
return 0;
}
四:结构体与数组,指针,typedef关键字的联合使用
1.结构体数组
#include <stdio.h>
//定义结构体类型
struct student{
int sno;
char name[20];
float score;
};
int main(void)
{
struct student st[5];
int i;
for(i = 0 ; i < 5; i++){
printf("请输入学生信息:");
scanf("%d%s%f",&st[i].sno,st[i].name,&st[i].score);
}
for(i = 0 ; i < 5; i++)
printf("%d %s %.2f\n",st[i].sno,st[i].name,st[i].score);
return 0;
}
2.结构体指针
#include <stdio.h>
//定义结构体类型
struct student{
int sno;
char name[20];
float score;
};
int main(void)
{
struct student st1 = {1001,"鹿晗",89.87};
struct student * p; //p为指针变量,可以保存结构体变量的地址
p = &st1;
printf("%d %s %.2f\n",st1.sno,st1.name,st1.score);
printf("%d %s %.2f\n",(*p).sno,(*p).name,(*p).score);
printf("%d %s %.2f\n",p->sno,p->name,p->score); //p必须指向结构体,才能取成员
return 0;
}
3.与typedef关键字的联合使用
1.typedef关键字
1.1,作用:
给数据类型引入新的别名。
1.2,用法
语法格式:
typedef 定义数据的格式;
例如:
typedef int a; //给int取别名a
typedef int b,c; //b和c也是int的别名
typedef struct student{
int sno;
char name[20];
float score;
}st,*sp; //st是结构体类型struct student的别名
//sp是结构体类型strust student * 的别名
//sp p 等价于 struct student *p
1.3,实例
#include <stdio.h>
typedef int a;
typedef int b,c; //b和c也是int的别名
typedef int arr[5]; //arr是数组类型的别名
typedef int* p; //int*的别名是p
typedef struct student{
int sno;
char name[20];
float score;
}st,*sp; //st是结构体类型struct student的别名
//sp是结构体类型strust student * 的别名
//sp p 等价于 struct student *p
int main(void)
{
a a1 = 100,a2 = 200;
arr array = {1,2,3,4,5};
b i;
p p1 = &a1;
st s1 = {1001,"Jack",89.56},s2 = {1002,"Rose",77.67};
sp ps1 = &s1,ps2 = &s2;
printf("a1 = %d,a2 = %d\n",a1,a2);
for(i = 0; i < 5; i++)
printf("%d ",array[i]);
printf("\n");
printf("*p1 = %d\n",*p1);
printf("%d %s %.2f\n",s1.sno,s1.name,s1.score);
printf("%d %s %.2f\n",ps1->sno,ps1->name,ps1->score);
printf("%d %s %.2f\n",s2.sno,s2.name,s2.score);
printf("%d %s %.2f\n",ps2->sno,ps2->name,ps2->score);
return 0;
}