乐闻世界logo
搜索文章和话题

C语言中结构体和类的区别及使用场景是什么?

2月18日 17:22

C语言中结构体和类的区别及使用场景是什么?

结构体基本概念:

  1. 结构体定义

    c
    struct Point { int x; int y; }; struct Point p1 = {10, 20}; struct Point p2 = {.x = 30, .y = 40};
  2. 结构体指针

    c
    struct Point *ptr = &p1; ptr->x = 50; ptr->y = 60;

与类的对比:

  1. 数据封装

    c
    // C语言结构体 struct Rectangle { int width; int height; }; // C++类 class Rectangle { private: int width; int height; public: Rectangle(int w, int h) : width(w), height(h) {} int area() { return width * height; } };
  2. 方法定义

    c
    // C语言:使用函数指针 struct Shape { int (*area)(struct Shape*); int (*perimeter)(struct Shape*); }; int rectangle_area(struct Shape *s) { struct Rectangle *r = (struct Rectangle*)s; return r->width * r->height; } // C++:成员函数 class Shape { public: virtual int area() = 0; virtual int perimeter() = 0; };
  3. 构造和析构

    c
    // C语言:手动初始化 struct Point create_point(int x, int y) { struct Point p = {x, y}; return p; } void destroy_point(struct Point *p) { // 手动清理资源 } // C++:自动构造和析构 class Point { public: Point(int x, int y) : x(x), y(y) {} ~Point() {} private: int x, y; };

使用场景:

  1. 数据结构

    c
    struct Node { int data; struct Node *next; }; struct LinkedList { struct Node *head; int size; };
  2. 配置管理

    c
    struct Config { char server[256]; int port; int timeout; int max_connections; }; struct Config load_config(const char *filename);
  3. 网络协议

    c
    struct PacketHeader { unsigned int version : 4; unsigned int type : 4; unsigned int length : 16; unsigned int checksum : 8; };

高级特性:

  1. 结构体嵌套

    c
    struct Address { char street[100]; char city[50]; char country[50]; }; struct Person { char name[50]; int age; struct Address address; }; struct Person person = { .name = "John Doe", .age = 30, .address = { .street = "123 Main St", .city = "New York", .country = "USA" } };
  2. 结构体数组

    c
    struct Student { int id; char name[50]; float score; }; struct Student students[100]; students[0].id = 1; strcpy(students[0].name, "Alice"); students[0].score = 95.5;
  3. 灵活数组成员

    c
    struct String { size_t length; char data[]; // 灵活数组成员 }; struct String *create_string(const char *str) { size_t len = strlen(str); struct String *s = malloc(sizeof(struct String) + len + 1); s->length = len; strcpy(s->data, str); return s; }

最佳实践:

  1. 命名约定

    c
    struct Point { int x; int y; }; typedef struct Point Point; Point p1 = {10, 20};
  2. 初始化函数

    c
    struct Point point_create(int x, int y) { struct Point p = {x, y}; return p; } struct Point p = point_create(10, 20);
  3. 内存管理

    c
    struct Point *point_alloc(int x, int y) { struct Point *p = malloc(sizeof(struct Point)); if (p) { p->x = x; p->y = y; } return p; } void point_free(struct Point *p) { free(p); }

注意事项:

  1. 内存对齐

    c
    struct Example { char c; // 1字节 + 3字节填充 int i; // 4字节 }; // 总大小: 8字节
  2. 结构体赋值

    c
    struct Point p1 = {10, 20}; struct Point p2 = p1; // 浅拷贝
  3. 结构体比较

    c
    struct Point p1 = {10, 20}; struct Point p2 = {10, 20}; // 错误:不能直接比较结构体 // if (p1 == p2) { } // 正确:逐个成员比较 if (p1.x == p2.x && p1.y == p2.y) { printf("Equal\n"); }
标签:C语言