035. 编写结构体的函数

在C语言中,结构体(struct)是一种用户自定义的数据类型,用于将多个不同类型的数据组合在一起。可以编写函数来操作结构体,这些函数可以用于初始化结构体、访问结构体成员、修改结构体成员等。以下将通过具体示例展示如何编写和使用结构体的函数。

示例1:定义结构体和函数

假设我们有一个Person结构体,包含姓名、年龄和身高。我们将编写函数来初始化结构体、打印结构体信息和更新结构体成员。

定义结构体

#include <stdio.h>
#include <string.h>

// 定义一个结构体
struct Person {
    char name[50];
    int age;
    float height;
};

编写函数

初始化结构体

void initializePerson(struct Person *p, const char *name, int age, float height) {
    strcpy(p->name, name); // 使用 strcpy 复制字符串
    p->age = age;
    p->height = height;
}

打印结构体信息

void printPerson(const struct Person *p) {
    printf("Name: %s\n", p->name);
    printf("Age: %d\n", p->age);
    printf("Height: %.2f cm\n", p->height);
}

更新结构体成员

void updatePerson(struct Person *p, const char *name, int age, float height) {
    strcpy(p->name, name);
    p->age = age;
    p->height = height;
}

示例2:使用结构体和函数

主函数

int main() {
    struct Person person;

    // 初始化结构体
    initializePerson(&person, "Alice", 25, 165.5);
    printf("After initialization:\n");
    printPerson(&person);

    // 更新结构体
    updatePerson(&person, "Bob", 30, 175.0);
    printf("\nAfter update:\n");
    printPerson(&person);

    return 0;
}

输出结果

After initialization:
Name: Alice
Age: 25
Height: 165.50 cm

After update:
Name: Bob
Age: 30
Height: 175.00 cm

示例3:使用结构体数组和函数

假设我们有一个Person结构体数组,需要初始化并打印每个结构体的信息。

定义结构体数组

#include <stdio.h>
#include <string.h>

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

void initializePerson(struct Person *p, const char *name, int age, float height) {
    strcpy(p->name, name);
    p->age = age;
    p->height = height;
}

void printPerson(const struct Person *p) {
    printf("Name: %s\n", p->name);
    printf("Age: %d\n", p->age);
    printf("Height: %.2f cm\n", p->height);
}

主函数

int main() {
    struct Person people[3];

    // 初始化结构体数组
    initializePerson(&people[0], "Alice", 25, 165.5);
    initializePerson(&people[1], "Bob", 30, 175.0);
    initializePerson(&people[2], "Charlie", 22, 180.5);

    // 打印结构体数组
    for (int i = 0; i < 3; i++) {
        printf("Person %d:\n", i + 1);
        printPerson(&people[i]);
        printf("\n");
    }

    return 0;
}

输出结果

Person 1:
Name: Alice
Age: 25
Height: 165.50 cm

Person 2:
Name: Bob
Age: 30
Height: 175.00 cm

Person 3:
Name: Charlie
Age: 22
Height: 180.50 cm

示例4:使用结构体指针和函数

假设我们需要通过指针操作结构体,以下是一个示例。

定义结构体和函数

#include <stdio.h>
#include <string.h>

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

void initializePerson(struct Person *p, const char *name, int age, float height) {
    strcpy(p->name, name);
    p->age = age;
    p->height = height;
}

void printPerson(const struct Person *p) {
    printf("Name: %s\n", p->name);
    printf("Age: %d\n", p->age);
    printf("Height: %.2f cm\n", p->height);
}

主函数

int main() {
    struct Person person;

    // 初始化结构体
    initializePerson(&person, "Alice", 25, 165.5);

    // 使用指针访问和修改结构体成员
    struct Person *ptr = &person;
    printf("Before update:\n");
    printPerson(ptr);

    // 更新结构体成员
    strcpy(ptr->name, "Bob");
    ptr->age = 30;
    ptr->height = 175.0;

    printf("\nAfter update:\n");
    printPerson(ptr);

    return 0;
}

输出结果

Before update:
Name: Alice
Age: 25
Height: 165.50 cm

After update:
Name: Bob
Age: 30
Height: 175.00 cm

通过上述示例,你可以看到如何在C语言中编写和使用结构体的函数:

  1. 定义结构体:使用struct关键字定义结构体。
  2. 编写函数:编写函数来初始化结构体、访问结构体成员、修改结构体成员。
  3. 使用结构体数组:可以定义结构体数组,并通过循环初始化和打印每个结构体的信息。
  4. 使用结构体指针:可以通过指针操作结构体,实现更灵活的内存管理。

视频讲解

BiliBili: 视睿网络-哔哩哔哩视频 (bilibili.com)