【C语言篇】探索 C 语言结构体:从基础语法到数据组织的初体验

发布于:2024-12-07 ⋅ 阅读:(142) ⋅ 点赞:(0)

在这里插入图片描述
我的个人主页
我的专栏C语言,希望能帮助到大家!!!点赞❤ 收藏❤


目录
  1. 什么是结构体
  2. 结构体的定义与使用
  3. 结构体内存布局
  4. 嵌套结构体与指针
  5. 结构体数组的操作
  6. 结构体与函数
  7. 结构体内存对齐机制
  8. 位域与结构体的结合
  9. 动态内存分配与结构体
  10. 常见错误及调试技巧
  11. 实战案例:基于结构体的学生管理系统

1. 什么是结构体

在C语言中,结构体(struct)是一种用户定义的数据类型,用于将不同类型的变量组合在一起。它允许我们定义复杂的数据类型,便于更直观地表示现实中的对象。

关键点:

  • 结构体可以存储不同类型的数据。
  • 适合需要组织复杂数据的场景。

2. 结构体的定义与使用

2.1 定义结构体

定义一个结构体时需要使用关键字 struct。以下是基本语法:

#include <stdio.h>

struct Student {
    int id;          // 学号
    char name[50];   // 姓名
    float score;     // 成绩
};
2.2 声明和初始化结构体变量
struct Student student1 = {1, "Alice", 95.5};
2.3 访问结构体成员

通过点运算符(.)访问成员:

printf("学号: %d\n", student1.id);
printf("姓名: %s\n", student1.name);
printf("成绩: %.2f\n", student1.score);

3. 结构体内存布局

结构体在内存中的存储取决于其成员的排列顺序和对齐方式。以下代码用于分析内存布局:

#include <stdio.h>

struct Example {
    char c;
    int i;
    double d;
};

int main() {
    struct Example e;
    printf("结构体大小: %zu 字节\n", sizeof(e));
    return 0;
}

输出示例:

结构体大小: 16 字节

在这里插入图片描述


4. 嵌套结构体与指针

4.1 嵌套结构体
struct Address {
    char city[50];
    int zipCode;
};

struct Person {
    char name[50];
    struct Address address;
};
4.2 结构体指针
struct Student *ptr = &student1;
printf("姓名: %s\n", ptr->name); // 使用 -> 访问成员

5. 结构体数组的操作

结构体数组用于存储多个结构体变量:

struct Student students[3] = {
    {1, "Alice", 90.0},
    {2, "Bob", 85.5},
    {3, "Charlie", 88.0}
};

for (int i = 0; i < 3; i++) {
    printf("学号: %d, 姓名: %s, 成绩: %.2f\n", students[i].id, students[i].name, students[i].score);
}

6. 结构体与函数

6.1 传递结构体参数

按值传递会复制整个结构体,效率较低:

void printStudent(struct Student s) {
    printf("学号: %d, 姓名: %s, 成绩: %.2f\n", s.id, s.name, s.score);
}

按指针传递更高效:

void printStudentPtr(const struct Student *s) {
    printf("学号: %d, 姓名: %s, 成绩: %.2f\n", s->id, s->name, s->score);
}

7. 结构体内存对齐机制

C语言编译器会根据平台规定对结构体进行对齐优化,确保高效访问。

7.1 对齐规则
  • 每个成员的地址是其类型大小的整数倍。
  • 结构体的总大小是其最大对齐单位的倍数。

8. 位域与结构体的结合

位域允许我们压缩存储多个布尔值或小整数:

struct BitField {
    unsigned int a : 1;
    unsigned int b : 3;
    unsigned int c : 4;
};

9. 动态内存分配与结构体

结合 malloc 动态分配结构体内存:

#include <stdlib.h>

struct Student *student = (struct Student *)malloc(sizeof(struct Student));
student->id = 1;
strcpy(student->name, "Alice");
student->score = 95.0;
free(student);

10. 常见错误及调试技巧

  • 忘记为指针分配内存。
  • 忽略结构体大小对齐规则。
  • 在未初始化的结构体上操作。

11. 实战案例:基于结构体的学生管理系统

设计一个管理学生信息的程序,支持添加、删除、查询操作。

11.1 数据结构设计
struct Student {
    int id;
    char name[50];
    float score;
};
11.2 主函数实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

struct Student {
    int id;
    char name[50];
    float score;
};

struct Student students[MAX];
int count = 0;

void addStudent(int id, const char *name, float score) {
    students[count].id = id;
    strcpy(students[count].name, name);
    students[count].score = score;
    count++;
}

void printStudents() {
    for (int i = 0; i < count; i++) {
        printf("学号: %d, 姓名: %s, 成绩: %.2f\n", students[i].id, students[i].name, students[i].score);
    }
}

int main() {
    addStudent(1, "Alice", 90.5);
    addStudent(2, "Bob", 85.0);
    printStudents();
    return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到