当前位置: 首页 > article >正文

FX-结构体

1. 结构体的定义

结构体的定义使用 struct 关键字,语法如下:

struct 结构体名 {
    数据类型 成员1;
    数据类型 成员2;
    // 更多成员...
};


示例
struct Student {
    int id;          // 学号
    char name[20];  // 姓名
    float score;    // 成绩
};

2. 结构体变量的声明

定义结构体后,可以声明结构体变量。

方法1:先定义结构体,再声明变量

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

struct Student stu1, stu2; // 声明两个结构体变量

方法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, stu2; // 直接使用 Student 声明变量

3. 结构体成员的访问

结构体成员通过点运算符 . 访问。

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

int main() {
    struct Student stu1;
    stu1.id = 101;
    strcpy(stu1.name, "Alice");
    stu1.score = 95.5;

    printf("ID: %d, Name: %s, Score: %.2f\n", stu1.id, stu1.name, stu1.score);
    return 0;
}

4. 结构体指针

结构体指针用于指向结构体变量,通过 -> 运算符访问成员。

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

int main() {
    struct Student stu1 = {101, "Alice", 95.5};
    struct Student *p = &stu1; // 定义结构体指针

    printf("ID: %d, Name: %s, Score: %.2f\n", p->id, p->name, p->score);
    return 0;
}

5. 结构体数组

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

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

int main() {
    struct Student stu[3] = {
        {101, "Alice", 95.5},
        {102, "Bob", 88.0},
        {103, "Charlie", 92.3}
    };

    for (int i = 0; i < 3; i++) {
        printf("ID: %d, Name: %s, Score: %.2f\n", stu[i].id, stu[i].name, stu[i].score);
    }
    return 0;
}

6. 结构体嵌套

结构体可以嵌套其他结构体。

struct Date {
    int year;
    int month;
    int day;
};

struct Student {
    int id;
    char name[20];
    struct Date birthday; // 嵌套结构体
};

int main() {
    struct Student stu1 = {101, "Alice", {2000, 10, 15}};
    printf("Birthday: %d-%d-%d\n", stu1.birthday.year, stu1.birthday.month, stu1.birthday.day);
    return 0;
}

7. 结构体作为函数参数

结构体可以作为函数参数传递,可以按值传递,也可以按指针传递。

按值传递
void printStudent(struct Student stu) {
    printf("ID: %d, Name: %s, Score: %.2f\n", stu.id, stu.name, stu.score);
}
按指针传递
void printStudent(struct Student *p) {
    printf("ID: %d, Name: %s, Score: %.2f\n", p->id, p->name, p->score);
}

8. 结构体的大小

结构体的大小由所有成员的大小决定,但可能会因为内存对齐而增加。

struct Example {
    char a;    // 1 字节
    int b;     // 4 字节
    double c;  // 8 字节
};

int main() {
    printf("Size of struct Example: %lu\n", sizeof(struct Example)); // 可能输出 16(内存对齐)
    return 0;
}

9. 结构体的初始化

结构体可以在声明时初始化。

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

int main() {
    struct Student stu1 = {101, "Alice", 95.5}; // 初始化
    return 0;
}

10. 结构体的常见用途

  • 表示复杂的数据结构(如链表、树等)。

  • 存储数据库记录。

  • 封装多个相关的数据项。


练习题

  1. 定义一个结构体 Point,包含 x 和 y 两个成员,表示二维坐标。编写函数计算两点之间的距离。

  2. 定义一个结构体 Book,包含书名、作者和价格。编写程序输入 5 本书的信息并输出最贵的一本书。


通过复习结构体的定义、使用和常见操作,可以更好地掌握C语言中复杂数据类型的处理能力。


http://www.kler.cn/a/573920.html

相关文章:

  • FusionInsight MRS云原生数据湖
  • 江科大51单片机笔记【8】LED点阵屏显示图形及动画(下)
  • Hive-优化(参数优化篇)
  • 扩散语言模型:从图像生成到文本创造的范式跃迁
  • 随手记录第十六话 -- Go 语言入门:基础知识与常用框架
  • 计算机网络开发(2)TCP\UDP区别、TCP通信框架、服务端客户端通信实例
  • 每天一个Flutter开发小项目 (13) : 本地数据随心存取 - 构建SQLite ToDo 应用,掌握Flutter本地数据库操作
  • 算法·搜索
  • 《张一鸣,创业心路与算法思维》
  • 汽车AI识别及处理解决方案,引领未来驾驶新风尚
  • MybatisPlus从入门到精通
  • Linux基础 IO 和文件
  • Git安装与配置
  • 游戏树搜索与优化策略:Alpha-Beta剪枝及其实例分析
  • AI自动化应用的影响
  • IDC权威认证!永洪科技入选 IDC「GBI图谱」,点亮生成式 BI 价值灯塔
  • Redis的CPU高达90%时如何处理
  • STM32G030F6P6详细解读
  • upload-labs靶场 1-21通关
  • Go 语言中 panic 和 recover 的代价:性能与设计的权衡