Understand the application and usage of structures in programming

 

Table of contents

1. Basic concept of structure

2. Declaration and initialization of structure

3. Nesting of structures and arrays

4. Structures and pointers

5. Usage examples of structures: student performance management system


In computer programming, Struct is an important data type that allows us to organize and store different types of data and define custom data structures for these data. This blog will introduce the usage of structures in depth so that you can better understand and utilize this concept.

1. Basic concepts of structures

A structure is a user-defined data type that allows you to combine different types of data together to form a single unit of data. Unlike simple data types (integers, floating point numbers, etc.), structures can contain multiple fields, and each field can be of a different data type.

struct Person {
    char name[50];
    int age;
    float height;
};

2. Declaration and initialization of structure

Before using a structure, you need to declare it and allocate memory space for it. Initialization of a structure can be done by direct assignment or by using a member initialization list.

struct Person person1;  // 声明一个名为 person1 的 Person 结构体
person1.age = 25;       // 初始化 age 字段
strcpy(person1.name, "John Doe");  // 初始化 name 字段
person1.height = 175.5;  // 初始化 height 字段

 Or use a member initialization list:

struct Person person2 = {"Jane Doe", 30, 162.3};

3. Nesting and array of structures

Structures can be nested within other structures to form complex data structures. This nested approach can better organize and represent complex relationships in the real world.

struct Address {
    char city[50];
    char street[100];
};

struct Contact {
    struct Person personInfo;
    struct Address addressInfo;
};

4. Structures and pointers

The pointer of the structure allows us to dynamically allocate memory and reduce unnecessary waste of resources.

 

struct Person *ptrPerson = malloc(sizeof(struct Person));
ptrPerson->age = 28;
strcpy(ptrPerson->name, "Alice");
ptrPerson->height = 160.2;

5. Usage example of structure: student performance management system

Through a simple example of a student performance management system, the usage of the structure in practical applications is demonstrated. This includes storage, addition, deletion and query operations of student information.

struct Student {
    int studentID;
    char name[50];
    float grade;
};

// 函数声明
void addStudent(struct Student students[], int *count, struct Student newStudent);
void removeStudent(struct Student students[], int *count, int studentID);
void printStudentInfo(struct Student students[], int count);

// 主函数
int main() {
    struct Student allStudents[100];
    int studentCount = 0;

    struct Student newStudent1 = {101, "Bob", 85.5};
    struct Student newStudent2 = {102, "Alice", 92.0};

    addStudent(allStudents, &studentCount, newStudent1);
    addStudent(allStudents, &studentCount, newStudent2);

    printStudentInfo(allStudents, studentCount);

    removeStudent(allStudents, &studentCount, 101);

    printStudentInfo(allStudents, studentCount);

    return 0;
}

Guess you like

Origin blog.csdn.net/m0_74053536/article/details/134431045