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

What are the differences and usage scenarios between structures and classes in C language?

2月18日 17:22

What are the differences and usage scenarios between structures and classes in C language?

Structure Basic Concepts:

  1. Structure Definition

    c
    struct Point { int x; int y; }; struct Point p1 = {10, 20}; struct Point p2 = {.x = 30, .y = 40};
  2. Structure Pointers

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

Comparison with Classes:

  1. Data Encapsulation

    c
    // C language structure struct Rectangle { int width; int height; }; // C++ class class Rectangle { private: int width; int height; public: Rectangle(int w, int h) : width(w), height(h) {} int area() { return width * height; } };
  2. Method Definition

    c
    // C language: Use function pointers 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++: Member functions class Shape { public: virtual int area() = 0; virtual int perimeter() = 0; };
  3. Construction and Destruction

    c
    // C language: Manual initialization struct Point create_point(int x, int y) { struct Point p = {x, y}; return p; } void destroy_point(struct Point *p) { // Manual resource cleanup } // C++: Automatic construction and destruction class Point { public: Point(int x, int y) : x(x), y(y) {} ~Point() {} private: int x, y; };

Usage Scenarios:

  1. Data Structures

    c
    struct Node { int data; struct Node *next; }; struct LinkedList { struct Node *head; int size; };
  2. Configuration Management

    c
    struct Config { char server[256]; int port; int timeout; int max_connections; }; struct Config load_config(const char *filename);
  3. Network Protocols

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

Advanced Features:

  1. Nested Structures

    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. Structure Arrays

    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. Flexible Array Member

    c
    struct String { size_t length; char data[]; // Flexible array member }; 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; }

Best Practices:

  1. Naming Conventions

    c
    struct Point { int x; int y; }; typedef struct Point Point; Point p1 = {10, 20};
  2. Initialization Functions

    c
    struct Point point_create(int x, int y) { struct Point p = {x, y}; return p; } struct Point p = point_create(10, 20);
  3. Memory Management

    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); }

Important Considerations:

  1. Memory Alignment

    c
    struct Example { char c; // 1 byte + 3 bytes padding int i; // 4 bytes }; // Total size: 8 bytes
  2. Structure Assignment

    c
    struct Point p1 = {10, 20}; struct Point p2 = p1; // Shallow copy
  3. Structure Comparison

    c
    struct Point p1 = {10, 20}; struct Point p2 = {10, 20}; // Wrong: Cannot compare structures directly // if (p1 == p2) { } // Correct: Compare member by member if (p1.x == p2.x && p1.y == p2.y) { printf("Equal\n"); }
标签:C语言