What is a Struct?什么是结构体?
A struct is a composite data type in C that groups fields of different types into a logical whole. It predates the idea of “classes” in OOP and is useful for modeling entities.
结构体(struct)是 C 语言中的一种复合数据类型,它允许我们将不同类型的数据组合在一起,形成一个逻辑整体。它是面向对象编程中“类”概念的前身,适合建模实体。
Struct Memory Layout Example:结构体内存布局示例:
+------------------+
| red (int) | 偏移量: 0字节
+------------------+
| green (int) | 偏移量: 4字节
+------------------+
| blue (int) | 偏移量: 8字节
+------------------+
| 总大小: 12字节 |
+------------------+
struct rgb {
int red;
int green;
int blue;
};
结构体的基本操作
1. 声明结构体变量
struct rgb color1;
struct rgb color2 = {255, 128, 0};
2. 访问结构体成员
struct rgb my_color;
my_color.red = 255;
my_color.green = 128;
my_color.blue = 0;
printf("RGB: %d, %d, %d\n",
my_color.red, my_color.green, my_color.blue);
3. 结构体指针
struct rgb color = {10, 20, 30};
struct rgb *ptr = &color;
ptr->red = 100;
ptr->green = 200;
💡 点运算符 vs 箭头运算符
- . (dot): access members via a struct variable. (点运算符): 用于结构体变量访问成员
- -> (arrow): access members via a struct pointer-> (箭头运算符): 用于结构体指针访问成员
- ptr->member is equivalent to (*ptr).memberptr->member 等价于 (*ptr).member
结构体作为函数参数
Structs can be passed to functions; be mindful of copying vs passing by pointer.结构体可以作为函数参数传递,但要注意传递方式的不同:
void print_color(struct rgb color) {
printf("RGB: %d, %d, %d\n",
color.red, color.green, color.blue);
}
void change_color(struct rgb *color) {
int temp = color->red;
color->red = color->blue;
color->blue = color->green;
color->green = temp;
}
🎯 RGB Struct Conversion ExampleRGB结构体转换实例
Let’s refactor the previous RGB conversion using a struct:让我们用结构体重构之前的RGB颜色转换函数:
struct rgb {
int red, green, blue;
};
void change_colour_struct(struct rgb *color) {
int temp = color->red;
color->red = color->blue;
color->blue = color->green;
color->green = temp;
}
结构体数组
Create arrays of structs to manage multiple related records:我们可以创建结构体数组来管理多个相关的数据:
struct movie_review {
char title[100];
int release_year;
int rating;
};
struct movie_review reviews[3] = {
{"The Matrix", 1999, 9},
{"Inception", 2010, 8},
{"Interstellar", 2014, 10}
};
for (int i = 0; i < 3; i++) {
printf("%s (%d): %d/10\n",
reviews[i].title,
reviews[i].release_year,
reviews[i].rating);
}
⚠️ 结构体内存对齐
Compilers may insert padding for alignment; total size can exceed sum of fields.编译器可能会在结构体中插入填充字节以满足内存对齐要求,这可能导致结构体的大小大于各成员大小之和。
struct example {
char c;
int i;
// 1字节
};
总结
Structs are crucial for organizing complex data. In this module, you learned:结构体是C语言中组织复杂数据的重要工具,本周我们学习了:
- Struct Definition: how to define and use struct types结构体定义:如何定义和使用结构体类型
- Member Access: difference between dot and arrow operators成员访问:点运算符和箭头运算符的区别
- Struct Parameters: choose value vs pointer passing结构体参数:值传递和指针传递的选择
- Array of Structs: manage multiple related records结构体数组:管理多个相关数据
- Practical Uses: RGB colors, movie reviews, etc.实际应用:RGB颜色、电影评论等实例
🚀 下一步学习
Structs enable organizing complex data. Next, we’ll learn dynamic allocation to create structs at runtime.结构体让我们能够组织复杂的数据,接下来我们将学习动态内存分配,看看如何在运行时创建结构体。