当然!下面是关于 C语言结构体数组(Structure Array) 的详细讲解,适合初学者到中级开发者掌握结构体数组的定义、初始化、使用、遍历、指针操作等。
🧠 C语言结构体数组详解
🧩 一、什么是结构体数组?
结构体数组是由多个结构体元素组成的数组,用于表示一组相同类型的复合数据。
就像 int a[10];
表示10个整数,结构体数组如:
struct Student students[10];
表示 10 个 Student
类型的结构体变量。
🏗️ 二、结构体数组定义与初始化
示例结构体:
struct Student {
int id;
char name[50];
float score;
};
✅ 定义结构体数组:
struct Student students[3]; // 表示3个学生
✅ 初始化结构体数组:
struct Student students[3] = {
{1001, "Alice", 85.5},
{1002, "Bob", 92.0},
{1003, "Charlie", 78.0}
};
🧪 三、结构体数组的访问与操作
访问单个元素:
printf("第一个学生名字: %s\n", students[0].name);
修改元素:
students[1].score = 95.0;
遍历结构体数组:
for (int i = 0; i < 3; i++) {
printf("ID: %d, 姓名: %s, 分数: %.1f\n",
students[i].id, students[i].name, students[i].score);
}
🧷 四、结构体数组与函数搭配使用
可以将结构体数组传递给函数处理,例如计算平均成绩:
float getAverage(struct Student s[], int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += s[i].score;
}
return sum / n;
}
调用:
float avg = getAverage(students, 3);
printf("平均分:%.2f\n", avg);
🧲 五、结构体数组与指针操作(进阶)
struct Student *p = students; // 指向第一个结构体元素
printf("第一个学生ID: %d\n", p->id); // 或者 (*p).id
(p+1)->score = 90.5; // 修改第二个学生分数
也可以这样遍历:
for (int i = 0; i < 3; i++) {
printf("姓名:%s\n", (p+i)->name);
}
🧩 六、常见应用场景
应用 | 示例 |
---|---|
成绩管理系统 | 一组学生成绩 |
库存/商品列表 | 一组商品结构体 |
日志系统 | 多个日志条目组成数组 |
图书管理系统 | 多本书籍信息结构体数组 |
🧱 七、完整示例代码
#include <stdio.h>
struct Student {
int id;
char name[50];
float score;
};
int main() {
struct Student students[3] = {
{1001, "Alice", 85.5},
{1002, "Bob", 92.0},
{1003, "Charlie", 78.0}
};
for (int i = 0; i < 3; i++) {
printf("学生ID: %d, 姓名: %s, 成绩: %.1f\n",
students[i].id, students[i].name, students[i].score);
}
return 0;
}
📚 八、补充:typedef 简化结构体数组书写
typedef struct {
int id;
char name[50];
float score;
} Student;
Student students[10];
这样就可以直接写 Student
,不需要 struct Student
。
好的!下面继续给你讲解 C语言结构体数组的排序、文件存储与动态分配,配有示例代码,助你全面掌握结构体数组实战技巧。
C语言结构体数组进阶教程:排序、文件存储与动态分配
一、结构体数组排序(按成绩降序)
使用 qsort()
函数,需实现比较函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id;
char name[50];
float score;
} Student;
// 比较函数:按score降序
int compareScore(const void *a, const void *b) {
Student *s1 = (Student*)a;
Student *s2 = (Student*)b;
if (s2->score > s1->score) return 1;
else if (s2->score < s1->score) return -1;
else return 0;
}
int main() {
Student students[3] = {
{1001, "Alice", 85.5},
{1002, "Bob", 92.0},
{1003, "Charlie", 78.0}
};
qsort(students, 3, sizeof(Student), compareScore);
printf("按成绩降序排序结果:\n");
for (int i = 0; i < 3; i++) {
printf("ID:%d, 姓名:%s, 成绩:%.1f\n", students[i].id, students[i].name, students[i].score);
}
return 0;
}
二、结构体数组文件存储
1. 写入文件(二进制写法)
FILE *fp = fopen("students.dat", "wb");
if (fp == NULL) {
perror("打开文件失败");
return 1;
}
fwrite(students, sizeof(Student), 3, fp);
fclose(fp);
2. 读取文件
Student buffer[3];
FILE *fp = fopen("students.dat", "rb");
if (fp == NULL) {
perror("打开文件失败");
return 1;
}
fread(buffer, sizeof(Student), 3, fp);
fclose(fp);
for (int i = 0; i < 3; i++) {
printf("ID:%d, 姓名:%s, 成绩:%.1f\n", buffer[i].id, buffer[i].name, buffer[i].score);
}
三、动态结构体数组(用 malloc 分配)
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
char name[50];
float score;
} Student;
int main() {
int n = 3;
Student *students = (Student*) malloc(n * sizeof(Student));
if (students == NULL) {
printf("内存分配失败\n");
return 1;
}
// 赋值
students[0] = (Student){1001, "Alice", 85.5};
students[1] = (Student){1002, "Bob", 92.0};
students[2] = (Student){1003, "Charlie", 78.0};
for (int i = 0; i < n; i++) {
printf("ID:%d, 姓名:%s, 成绩:%.1f\n", students[i].id, students[i].name, students[i].score);
}
free(students);
return 0;
}
四、小结
技术点 | 用法简述 |
---|---|
结构体数组排序 | 用 qsort() + 比较函数 |
文件写入读取 | 使用 fwrite() 和 fread() |
动态数组分配 | malloc() 和 free() 配合指针使用 |
发表回复