C语言结构体数组入门教程(非常详细)
所谓结构体数组,也就是由多个相同类型的结构体元素组成的数组;每个数组元素都是一个完整的结构体,包含了该结构体定义的所有成员。C语言的这种语法非常适合用来存储和处理具有多个属性的对象集合。
	
	例如,我们可以使用结构体数组来表示一个学生管理系统中的学生信息:
struct Student {
    char name[50];
    int age;
    float gpa;
};
struct Student class[30]; // 声明一个包含 30 个学生信息的结构体数组
C语言结构体数组的声明
声明结构体数组的语法与普通数组类似,只需在结构体类型名后面加上数组名和方括号,方括号中指定数组的大小:
struct StructName arrayName[arraySize];
我们也可以在声明结构体的同时直接声明结构体数组:
struct Book {
    char title[100];
    char author[50];
    int year;
} library[1000];
这里我们声明了一个名为 library 的结构体数组,它可以存储 1000 本书的信息。
C语言结构体数组的初始化
初始化结构体数组可以在声明时进行,也可以在声明后单独进行。以下是几种常见的初始化方法:
1. 声明时初始化
struct Point {
    int x;
    int y;
} points[3] = {
    {0, 0},
    {1, 1},
    {2, 2}
};
2. 使用指定初始化器(C99 标准及以后)
struct Employee {
    char name[50];
    int id;
    float salary;
} staff[3] = {
    [0] = {.name = "Alice", .id = 1001, .salary = 5000.0},
    [1] = {.name = "Bob", .id = 1002, .salary = 5500.0},
    [2] = {.name = "Charlie", .id = 1003, .salary = 6000.0}
};
3. 声明后逐个初始化
struct Car {
    char brand[20];
    char model[20];
    int year;
} cars[2];
strcpy(cars[0].brand, "Toyota");
strcpy(cars[0].model, "Corolla");
cars[0].year = 2020;
strcpy(cars[1].brand, "Honda");
strcpy(cars[1].model, "Civic");
cars[1].year = 2021;
访问结构体数组元素
	访问结构体数组的元素与访问普通数组类似,使用数组索引来选择特定的结构体,然后使用点运算符.来访问该结构体的成员:
#include <stdio.h>
struct Student {
    char name[50];
    int age;
    float gpa;
};
int main() {
    struct Student class[3] = {
        {"Alice", 20, 3.8},
        {"Bob", 21, 3.5},
        {"Charlie", 19, 3.9}
    };
    // 访问并打印第二个学生的信息
    printf("Name: %s\n", class[1].name);
    printf("Age: %d\n", class[1].age);
    printf("GPA: %.1f\n", class[1].gpa);
    return 0;
}
运行上述代码,输出结果为:
Name: Bob Age: 21 GPA: 3.5
C语言结构体数组的应用
结构体数组在实际编程中有广泛的应用,以下是一个更复杂的例子,展示了如何使用结构体数组来管理一个简单的图书馆系统:
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
    char title[100];
    char author[50];
    int year;
    int is_available;
};
struct Library {
    struct Book books[MAX_BOOKS];
    int book_count;
};
void add_book(struct Library *library, const char *title, const char *author, int year) {
    if (library->book_count < MAX_BOOKS) {
        struct Book *new_book = &library->books[library->book_count];
        strcpy(new_book->title, title);
        strcpy(new_book->author, author);
        new_book->year = year;
        new_book->is_available = 1;
        library->book_count++;
        printf("Book added successfully.\n");
    } else {
        printf("Library is full. Cannot add more books.\n");
    }
}
void display_books(const struct Library *library) {
    printf("Library Catalog:\n");
    for (int i = 0; i < library->book_count; i++) {
        const struct Book *book = &library->books[i];
        printf("%d. '%s' by %s (%d) - %s\n", 
               i + 1, 
               book->title, 
               book->author, 
               book->year, 
               book->is_available ? "Available" : "Checked out");
    }
}
int main() {
    struct Library my_library = {0};
    add_book(&my_library, "The Great Gatsby", "F. Scott Fitzgerald", 1925);
    add_book(&my_library, "To Kill a Mockingbird", "Harper Lee", 1960);
    add_book(&my_library, "1984", "George Orwell", 1949);
    display_books(&my_library);
    return 0;
}
运行这段代码,输出结果为:
Book added successfully. Book added successfully. Book added successfully. Library Catalog: 1. 'The Great Gatsby' by F. Scott Fitzgerald (1925) - Available 2. 'To Kill a Mockingbird' by Harper Lee (1960) - Available 3. '1984' by George Orwell (1949) - Available
这个例子展示了如何使用结构体数组来创建一个简单的图书馆系统。我们定义了 Book 结构体来表示单本书的信息,然后在 Library 结构体中使用 Book 结构体数组来存储多本书的信息。通过 add_book 和 display_books 函数,我们实现了添加书籍和显示图书馆目录的功能。
结构体数组的高级应用
结构体数组还可以与指针、动态内存分配和文件操作结合,实现更复杂的数据管理系统。例如,我们可以使用动态内存分配来创建可变大小的结构体数组,或者将结构体数组保存到文件中以实现数据持久化。
	
	以下是一个使用动态内存分配的结构体数组示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
    char name[50];
    int age;
};
int main() {
    int n;
    printf("Enter the number of people: ");
    scanf("%d", &n);
    // 动态分配结构体数组
    struct Person *people = (struct Person *)malloc(n * sizeof(struct Person));
    if (people == NULL) {
        printf("Memory allocation failed.\n");
        return 1;
    }
    // 输入人员信息
    for (int i = 0; i < n; i++) {
        printf("Enter name for person %d: ", i + 1);
        scanf("%s", people[i].name);
        printf("Enter age for person %d: ", i + 1);
        scanf("%d", &people[i].age);
    }
    // 显示人员信息
    printf("\nPeople Information:\n");
    for (int i = 0; i < n; i++) {
        printf("%d. Name: %s, Age: %d\n", i + 1, people[i].name, people[i].age);
    }
    // 释放动态分配的内存
    free(people);
    return 0;
}
这个例子展示了如何使用 malloc 函数动态分配结构体数组,允许用户在运行时决定数组的大小,这种方法特别适用于处理大量数据或者数据量在运行时才能确定的情况。
声明:《C语言系列教程》为本站“54笨鸟”官方原创,由国家机构和地方版权局所签发的权威证书所保护。
 
	