What are the differences and usage scenarios between structures and classes in C language?
Structure Basic Concepts:
-
Structure Definition
cstruct Point { int x; int y; }; struct Point p1 = {10, 20}; struct Point p2 = {.x = 30, .y = 40}; -
Structure Pointers
cstruct Point *ptr = &p1; ptr->x = 50; ptr->y = 60;
Comparison with Classes:
-
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; } }; -
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; }; -
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:
-
Data Structures
cstruct Node { int data; struct Node *next; }; struct LinkedList { struct Node *head; int size; }; -
Configuration Management
cstruct Config { char server[256]; int port; int timeout; int max_connections; }; struct Config load_config(const char *filename); -
Network Protocols
cstruct PacketHeader { unsigned int version : 4; unsigned int type : 4; unsigned int length : 16; unsigned int checksum : 8; };
Advanced Features:
-
Nested Structures
cstruct 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" } }; -
Structure Arrays
cstruct 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; -
Flexible Array Member
cstruct 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:
-
Naming Conventions
cstruct Point { int x; int y; }; typedef struct Point Point; Point p1 = {10, 20}; -
Initialization Functions
cstruct Point point_create(int x, int y) { struct Point p = {x, y}; return p; } struct Point p = point_create(10, 20); -
Memory Management
cstruct 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:
-
Memory Alignment
cstruct Example { char c; // 1 byte + 3 bytes padding int i; // 4 bytes }; // Total size: 8 bytes -
Structure Assignment
cstruct Point p1 = {10, 20}; struct Point p2 = p1; // Shallow copy -
Structure Comparison
cstruct 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"); }