结构体的概念不再赘述,参考各种C语言书籍(如谭浩强的C)
1 定义
struct 类名 {
成员列表(可以是变量,函数,结构体,共同体等类型);
} 变量名列表(包括数据,指针,普通变量等多种类型);
ex : 初始版
struct Point {
double x;
double y;
} P;
定义点的结构,包含两个成员x, y。
P为变量名,可以在结尾定义多个变量,如数组指针等。
ex : 拓展版
- 可以使用typedef 为结构体取更方便的别名
ex :
typedef struct Point {
double x;
double y;
} P; 之后便可以用P 代替struct Point 定义点类型的变量;
2 初始化结构体
- 在定义的时候初始化
ex :
#include <stdio.h>
struct Student {
char name[20];
int age;
}stu = {"zhangsan", 22}; // 此时如果前面有typedef会报错,具体原理待深挖。
int main () {
printf("%s %d", stu.name, stu.age);
return 0;
}
- 输入函数初始化
#include <stdio.h>
typedef struct Student {
char name[20];
int age;
}stu;
int main () {
stu a;
scanf("%s%d", &a.name, &a.age);
printf("%s %d", a.name, a.age);
return 0;
}
- 赋值初始化
ex :
#include <stdio.h>
#include <string.h>
typedef struct Student {
char name[20];
int age;
}stu;
int main () {
stu a;
char s[20];
int age;
scanf("%s%d", s, &age);
strcpy(a.name, s);
a.age = age;
printf("%s %d", a.name, a.age);
return 0;
}
3 引用结构体变量成员
- 非指针变量,变量.成员
sut.name;
- 指针变量, 变量-> 成员
stu -> name;
ex :
#include <stdio.h>
typedef struct Student {
char name[20];
int age;
}stu;
int main () {
stu a, c;
stu *b = &c;
// 指针b可以指向malloc开辟的地址,为方便此处指向已定义好的变量。
scanf("%s%d", &a.name, &a.age);
scanf("%s%d", &b -> name, &b -> age);
printf("%s %d\n", a.name, a.age);
printf("%s %d", b -> name, b -> age);
return 0;
}
ex : result
4 结构体数组
ex : 求三角形面积
#include <stdio.h>
const int N = 3;
typedef struct Point {
double x, y;
} PA[N];
double get_Area(PA p) {
return ((p[1].x - p[0].x) * (p[2].y - p[0].y) - (p[2].x - p[0].x) * (p[1].y - p[0].y)) / 2;
}
int main () {
PA points;
for (int i = 0; i < N; i ++) {
double x, y;
scanf("%lf%lf", &points[i].x, &points[i].y);
}
double area = get_Area(points);
printf("%lf", area);
return 0;
}
ex : result
5 结构体指针
ex : 指向结构体的指针
#include <stdio.h>
const int N = 3;
typedef struct Point {
double x, y;
} PA[N], *P, PO;
int main () {
PA points;
for (int i = 0; i < N; i ++) {
double x, y;
scanf("%lf%lf", &points[i].x, &points[i].y);
}
// 数组
P PI = &points[0];
// 等价写法 PI = points;
PO single;
single.x = 10.0;
single.y = 10.0;
P PII = &single;
printf("%lf %lf\n", PI -> x, PI -> y);
printf("%lf %lf", PII -> x, PII -> y);
return 0;
}
ex : result
ex : 指向结构体数组的指针
#include <stdio.h>
const int N = 3;
typedef struct Point {
double x, y;
} PA[N], *P, PO;
int main () {
PA points;
for (int i = 0; i < N; i ++) {
double x, y;
scanf("%lf%lf", &points[i].x, &points[i].y);
}
P pointer;
for (pointer = points; pointer < points + 3; pointer ++) {
printf("%lf %lf\n", pointer -> x, pointer -> y);
}
return 0;
}
6 结构体指针做函数参数
ex :
#include <stdio.h>
const int N = 3;
typedef struct Emp {
double salary;
double age;
}E[N], *PE;
// 指针接收数组参数
double get_av_age(PE e) {
double res = 0;
PE end = e + N;
for (; e < end; ++ e) {
res += e -> age;
}
return res / N;
}
// 数组接收数组参数
double get_av_sal(E e) {
double res = 0;
for (int i = 0; i < N; i ++) {
res += e[i].salary;
}
return res / N;
}
int main () {
E array;
for (int i = 0; i < N; i ++) {
scanf("%lf%lf", &array[i].salary, &array[i].age);
}
printf("%lf\n", get_av_age(array));
printf("%lf\n", get_av_sal(array));
return 0;
}
ex : result