【嵌入式学习笔记】基础拓展:C语言的结构体
本文总结了C语言结构体的基本使用方法,包括结构体定义(typedef和具名结构体两种方式)、变量声明与初始化(四种方法)、成员访问(点运算符和箭头运算符)、结构体指针操作以及结构体嵌套。重点讲解了如何通过typedef简化结构体使用,以及结构体成员的各种访问方式,为嵌入式开发中的数据结构处理提供了基础参考。文中示例代码展示了结构体从定义到使用的完整流程,特别强调了指针操作和嵌套结构体的处理方法。
·
前言
本系列学习笔记是本人跟随米醋电子工作室学习嵌入式的学习笔记,自用为主,不是教学或经验分享,若有误,大佬轻喷,同时欢迎交流学习,侵权即删。
一、结构体定义
1.1 基本语法
基本定义
struct 结构体名 {
数据类型 成员1;
数据类型 成员2;
// ... 更多成员
};
1.2 使用typedef简化
typedef struct {
int id;
char name[20];
float score;
} Student;
上面两种定义的基本区别
方式1:typedef 匿名结构体
typedef struct {
int id;
char name[20];
float score;
} Student; // Student是结构体类型的别名
// 使用方式:
Student stu1; // 直接使用别名,不需要struct关键字
Student *pStu; // 指针声明也很简洁
方式2:具名结构体
struct Student { // Student是结构体标签(tag)
int id;
char name[20];
float score;
};
// 使用方式:
struct Student stu1; // 必须使用struct关键字
struct Student *pStu; // 指针声明也需要struct
二、结构体变量声明与初始化
2.1 声明结构体变量
// 方式1:先定义,后声明
struct Student {
int id;
char name[20];
float score;
};
struct Student stu1; // 声明变量
// 方式2:定义时直接声明
struct Student {
int id;
char name[20];
float score;
} stu1, stu2; // 直接声明两个变量
// 方式3:使用typedef别名
typedef struct {
int id;
char name[20];
float score;
} Student;
Student stu1; // 简洁的声明方式
2.2 初始化结构体
// 方法1:声明时初始化(按顺序)
Student stu1 = {1001, "张三", 85.5};
// 方法2:声明时指定成员初始化(C99标准)
Student stu2 = {
.id = 1002,
.name = "李四",
.score = 90.0
};
// 方法3:先声明,后单独赋值
Student stu3;
stu3.id = 1003;
strcpy(stu3.name, "王五"); // 字符串需要复制
stu3.score = 77.5;
// 方法4:嵌入式常见初始化(清零)
Student stu4 = {0}; // 所有成员初始化为0
三、结构体成员访问
3.1 点运算符(普通变量)
Student stu = {1001, "张三", 85.5};
// 读取成员值
int id = stu.id;
char* name = stu.name;
float score = stu.score;
// 修改成员值
stu.id = 2001;
strcpy(stu.name, "张三丰");
stu.score = 92.5;
// 打印成员值
printf("学号: %d\n", stu.id);
printf("姓名: %s\n", stu.name);
printf("成绩: %.1f\n", stu.score);
3.2 箭头运算符(指针变量)
Student stu = {1001, "张三", 85.5};
Student *pStu = &stu; // 获取结构体指针
// 通过指针访问成员
printf("学号: %d\n", pStu->id); // 使用->运算符
printf("姓名: %s\n", pStu->name);
printf("成绩: %.1f\n", pStu->score);
// 修改成员值
pStu->id = 2001;
strcpy(pStu->name, "张三丰");
pStu->score = 92.5;
// 等价写法(不常用)
printf("学号: %d\n", (*pStu).id); // 先解引用,再用点运算符
四、结构体指针
// 定义结构体指针
Student stu = {1001, "张三", 85.5};
Student *pStu = &stu;
// 指针操作
printf("结构体大小: %zu 字节\n", sizeof(Student));
printf("结构体地址: %p\n", (void*)&stu);
printf("指针存储的地址: %p\n", (void*)pStu);
printf("指针本身地址: %p\n", (void*)&pStu);
五、结构体嵌套
基本嵌套
// 定义日期结构体
typedef struct {
int year;
int month;
int day;
} Date;
// 定义学生结构体,包含日期
typedef struct {
int id;
char name[20];
Date birthday; // 嵌套结构体
float score;
} Student;
// 初始化嵌套结构体
Student stu = {
.id = 1001,
.name = "张三",
.birthday = {2000, 5, 20}, // 嵌套初始化
.score = 85.5
};
// 访问嵌套成员
printf("出生年份: %d\n", stu.birthday.year);
printf("出生月份: %d\n", stu.birthday.month);
printf("出生日期: %d\n", stu.birthday.day);
// 修改嵌套成员
stu.birthday.year = 2001;
六、结构体数组
// 定义结构体数组
Student class[3] = {
{1001, "张三", 85.5},
{1002, "李四", 90.0},
{1003, "王五", 77.5}
};
// 遍历结构体数组
for (int i = 0; i < 3; i++) {
printf("学生%d: %s, 成绩: %.1f\n",
class[i].id, class[i].name, class[i].score);
}
// 结构体数组排序(按成绩)
#include <stdlib.h>
int compareScore(const void *a, const void *b) {
Student *stuA = (Student *)a;
Student *stuB = (Student *)b;
if (stuA->score < stuB->score) return 1;
if (stuA->score > stuB->score) return -1;
return 0;
}
qsort(class, 3, sizeof(Student), compareScore);
更多推荐
所有评论(0)