6.3 结构数组

Arrays of Structures as a simple database

结构

struct Student {
    char name[50];
    int age;
    float marks;
};

结构数组

struct Student students[100];
6.3.1 array-struct.c
#include <stdio.h>

// 定义学生结构体
struct Student {
    char * name;
    int age;
    float marks;
};
/* 
  ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ char * name 为什么 name变量不会重复定义?                                                                                   │
  │ 当你创建一个 struct Student 类型的变量时,每个变量都会有自己的 name 成员。                                                                │
  │ 这些 name 成员是独立的,它们可以指向不同的字符串,所以不会出现重复定义的问题。                                                                      │
  └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
 */
int main() {
    // 定义包含100个学生的结构数组
    struct Student students[100];
    
    // 对第一个学生的信息进行赋值
    students[0].name = "Alice";
    students[0].age = 20;
    students[0].marks = 85.5;

    
    // 对第二个学生的信息进行赋值
    students[1].name = "Bob";
    students[1].age = 22;
    students[1].marks = 78.2;

    // 打印第一个学生的信息
    printf("Student 1:\n");
    printf("Name: %s\n", students[0].name);
    printf("Age: %d\n", students[0].age);
    printf("Marks: %.2f\n", students[0].marks);

    // 打印第二个学生的信息
    printf("\nStudent 2:\n");
    printf("Name: %s\n", students[1].name);
    printf("Age: %d\n", students[1].age);
    printf("Marks: %.2f\n", students[1].marks);

    return 0;
}

/* 
  ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ 输出:                                                                                                             │
  │                                                                                                                 │
  │ Student 1:
  | Name: Alice
  | Age: 20
  | Marks: 85.50
  | 
  | Student 2:
  | Name: Bob
  | Age: 22
  | Marks: 78.20
  | 
  |                                                                                                     │
  └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
 */ 

使用 strcpy()函数和结构数组进行 demo:

6.3.1 strcpy-usage.c
#include <stdio.h>
#include <string.h>

// 定义一个包含姓名和年龄的结构体
struct Person {
    char name[50];
    int age;
};

int main() {
    // 定义一个包含3个人的结构数组
    struct Person people[3];

    // 使用strcpy()将字符串复制到结构体的字符数组成员中
    strcpy(people[0].name, "Alice"); //将字符串 "Alice" 复制到 people[0].name 指向的内存区域
    people[0].age = 25;

    strcpy(people[1].name, "Bob");
    people[1].age = 30;

    strcpy(people[2].name, "Charlie");
    people[2].age = 22;

    // 先遍历结构数组,后打印结构数组中的每个人的信息
    for (int i = 0; i < 3; ++i) {
        printf("Person %d:\n", i+1);
        printf("Name: %s\n", people[i].name);
        printf("Age: %d\n", people[i].age);
        printf("\n");
    }

    return 0;
}

/* 
  ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ 输出:                                                                                                             │
  │                                                                                                                 │
  │ Person 1:                                                                                                       │
  │ Name: Alice                                                                                                     │
  │ Age: 25                                                                                                         │
  │                                                                                                                 │
  │ Person 2:                                                                                                       │
  │ Name: Bob                                                                                                       │
  │ Age: 30                                                                                                         │
  │                                                                                                                 │
  │ Person 3:                                                                                                       │
  │ Name: Charlie                                                                                                   │
  │ Age: 22                                                                                                         │
  └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
 */

Last updated