结构体
预备知识
C语言有五种基本数据类型:
字符型 char,整型 int,单精度实数型 float,双精度实数型 double,空类型 void;要想创建一种新的数据类型要怎么做呢?这时候就需要构造数据类型了,就需要用结构体构造。
在这之前,先了解typedef的用法。
typedef的基本使用代码
#include<stdio.h>
typedef int z;
//为int再取一个名字,叫z; int 和 z 在后面都可以使用。
int main(void)
{
z i = 10;//这一句等价于 int i = 10;代码更简洁;
printf("%d\n", i);
return 0;
}
一、结构体的定义
第一种定义
struct 结构体名
{
成员项表列;
};
代码:
#include<stdio.h>
//定义一个数据类型,这个数据类型里面有三个成员
struct person//struct person就如同int
{
char name[12];
int age;
char sex;
};
int main(void)
{
struct person per1;//用这个数据类型定义一个变量per1, 这个就是结构体变量
per1 = {"张三",18,'B'};//为变量初始化
printf("名字:%s 年龄:%d 性别:%c", per1.name, per1.age, per1.sex);
return 0;
}
第二种定义
#include<stdio.h>
//定义一个数据类型,这个数据类型里面有三个成员
struct person//struct person就如同int
{
char name[12];
int age;
char sex;
}per1;//用这个数据类型定义一个变量per1, 这个就是结构体变量
int main(void)
{
per1 = {"张三",19,'B'};//为变量初始化
printf("名字:%s 年龄:%d 性别:%c", per1.name, per1.age, per1.sex);
return 0;
}
第三种定义
这里没有为结构体定义名字,所以不能定义其他变量。
#include<stdio.h>
struct
{
char name[12];
int age;
char sex;
}per1;//定义变量per1为结构体变量类型
int main(void)
{
per1 = {"张三",20,'B'};//为变量初始化
printf("名字:%s 年龄:%d 性别:%c", per1.name, per1.age, per1.sex);
return 0;
}
二、结构体变量的初始化
前面的可以这种也可以。
变量的初始化
#include<stdio.h>
//定义一个数据类型,这个数据类型里面有三个成员
struct person//struct person就如同int
{
char name[12];
int age;
char sex;
}per1 = {"张三", 21, 'B'};//用这个数据类型定义一个结构体变量per1并且初始化。
int main(void)
{
printf("名字:%s 年龄:%d 性别:%c", per1.name, per1.age, per1.sex);
return 0;
}
三、结构体变量的引用
格式:<结构体变量名>.<成员名> 或者 <(结构体指针变量名)>.<成员名> 或者 <结构体指针变量名>-> <成员名>
代码:
#include<stdio.h>
//定义一个数据类型,这个数据类型里面有三个成员
struct person//struct person就如同int
{
char name[12];
int age;
char sex;
}per1;//用这个数据类型定义一个结构体变量per1并且初始化。
int main(void)
{
per1 = { "张三", 22, 'B' };
struct person * per = &per1;//将per1的地址给per,per只能存放struct person型的地址
printf("名字:%s 年龄:%d 性别:%c", per1.name, (*per).age, per->sex);//三种引用
return 0;
}
用引用初始化
#include<stdio.h>
#include<string.h>//需要这个头文件
//定义一个数据类型,这个数据类型里面有三个成员
struct person//struct person就如同int
{
char name[12];
int age;
char sex;
}per1;//用这个数据类型定义一个结构体变量per1并且初始化。
int main(void)
{
struct person * per = &per1;
strcpy_s(per->name, "张三");
per->age = 23;
per->sex = 'B';
printf("名字:%s 年龄:%d 性别:%c", per1.name, (*per).age, per->sex);//(*per).age等价于per->age
return 0;
}
四、通过函数完成结构体变量的输入与输出
就是结构体变量作为函数参数传递
代码:
#include<stdio.h>
#include<string.h>
struct Student
{
int age;
char name[30];
double score;
};
void InputStudent(struct Student * pstu);//声明
void OutputStudent(struct Student a);
int main(void)
{
struct Student s1;//声明一个变量
InputStudent(&s1);//要改变,就要取地址
OutputStudent(s1);//只输出,赋值就行
//发st的地址也行,发st的内容也行
return 0;
}
void InputStudent(struct Student * pstu)//pstu占四个字节
{
(*pstu).age = 40;
strcpy(pstu->name,"张三");
(*pstu).score = 23.43;
}
void OutputStudent(struct Student a)
{
printf("%d %s %lf\n", a.age, a.name, a.score);
}
五、typedef与结构体一起使用
我们发现每次定义一个结构体变量或结构体变量指针是都要写“ struct 结构体名 ”,感觉就很麻烦很长,所以可以用typedef一起使用,就更简洁。
#include<stdio.h>
#include<string.h>
typedef struct Student
{
char name[30];
int age;
float score;
}*PST, ST;
//PST等价于 struct Student *, ST等价于 struct Student
int main(void)
{
ST st;//定义一个结构体变量, 名字为 st
PST ps = &st;//将st的地址给ps
strcpy_s(ps->name, "张三");
ps->age = 18;
ps->score = 95.5;
printf("%s %d %f", st.name, st.age, st.score);
return 0;
}
六 、应用
在VC++6.0可正常运行;动态构造一个数组存储学生信息,然后按分数排序输出。
#include<stdio.h>
#include<malloc.h>
typedef struct Student
{
int age;
int score;
char name[30];
}ST;
int main(void)
{
int len, i, j;
ST t;
ST * pArr;
printf("请输入学生的个数:");
printf("len = ");
scanf("%d", &len);
pArr = (ST * )malloc(len * sizeof(ST));
for(i = 0; i < len ; ++i)//输入
{
printf("请输入第%d个学生信息\n", i+1);
printf("age = ");
scanf("%d", &pArr[i].age);
printf("score = ");
scanf("%d", &pArr[i].score);
printf("name = ");
scanf("%s", pArr[i].name);
}
//排序
for(i = 0; i < len-1; ++i)
{
for(j = 0; j < len-1-i; ++j)
{
if(pArr[j].score < pArr[j +1].score)//比成绩
{
t = pArr[j];//换整体
pArr[j] = pArr[j + 1];
pArr[j + 1] = t;
}
}
}
printf("\n\n学生信息为:\n");
for(i = 0; i < len ; ++i)//输出
{
printf("第%d个学生的信息:\n", i + 1);
printf("age = %d\n", pArr[i].age);
printf("score = %d\n", pArr[i].score);
printf("name = %s\n", pArr[i].name);
printf("\n");
}
return 0;
}
后记:笔记不是很完善,有待完善。