C语言结构体

结构体详解

结构体是C语言中用于组织相关数据的重要特性,允许将不同类型的数据组合在一起。

结构体的主要特点:

  • 结构体定义和声明
  • 访问结构体成员
  • 结构体数组
  • 结构体指针
  • 嵌套结构体
  • 结构体作为函数参数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

// 嵌套结构体定义
struct Date {
    int year;
    int month;
    int day;
};

struct Employee {
    struct Person person;
    struct Date hireDate;
    float salary;
    char department[50];
};

// 函数声明
void printPerson(struct Person p);
void updatePerson(struct Person *p);
void printEmployee(struct Employee e);
struct Employee* createEmployee();
void freeEmployee(struct Employee *e);
void sortEmployees(struct Employee arr[], int size);
void searchEmployee(struct Employee arr[], int size, const char *name);

int main() {
    // 基本结构体使用
    printf("基本结构体使用示例:\n");
    struct Person person1 = {"张三", 25, 175.5, "北京市"};
    printPerson(person1);
    
    // 结构体指针
    printf("\n结构体指针示例:\n");
    struct Person *pPtr = &person1;
    printf("通过指针访问:%s, %d岁\n", pPtr->name, pPtr->age);
    
    // 更新结构体
    printf("\n更新结构体示例:\n");
    updatePerson(&person1);
    printPerson(person1);
    
    // 结构体数组
    printf("\n结构体数组示例:\n");
    struct Person people[3] = {
        {"李四", 30, 180.0, "上海市"},
        {"王五", 28, 178.5, "广州市"},
        {"赵六", 35, 182.0, "深圳市"}
    };
    
    for (int i = 0; i < 3; i++) {
        printPerson(people[i]);
    }
    
    // 嵌套结构体
    printf("\n嵌套结构体示例:\n");
    struct Employee emp1 = {
        {"张三", 25, 175.5, "北京市"},
        {2020, 1, 1},
        8000.0,
        "技术部"
    };
    printEmployee(emp1);
    
    // 动态分配结构体
    printf("\n动态分配结构体示例:\n");
    struct Employee *emp2 = createEmployee();
    if (emp2 != NULL) {
        strcpy(emp2->person.name, "李四");
        emp2->person.age = 30;
        emp2->person.height = 180.0;
        strcpy(emp2->person.address, "上海市");
        emp2->hireDate.year = 2019;
        emp2->hireDate.month = 6;
        emp2->hireDate.day = 15;
        emp2->salary = 10000.0;
        strcpy(emp2->department, "市场部");
        
        printEmployee(*emp2);
        freeEmployee(emp2);
    }
    
    // 结构体数组排序
    printf("\n结构体数组排序示例:\n");
    struct Employee employees[3] = {
        {
            {"张三", 25, 175.5, "北京市"},
            {2020, 1, 1},
            8000.0,
            "技术部"
        },
        {
            {"李四", 30, 180.0, "上海市"},
            {2019, 6, 15},
            10000.0,
            "市场部"
        },
        {
            {"王五", 28, 178.5, "广州市"},
            {2021, 3, 1},
            9000.0,
            "销售部"
        }
    };
    
    printf("排序前:\n");
    for (int i = 0; i < 3; i++) {
        printEmployee(employees[i]);
    }
    
    sortEmployees(employees, 3);
    
    printf("\n按年龄排序后:\n");
    for (int i = 0; i < 3; i++) {
        printEmployee(employees[i]);
    }
    
    // 结构体搜索
    printf("\n结构体搜索示例:\n");
    searchEmployee(employees, 3, "李四");
    
    return 0;
}

// 函数定义
void printPerson(struct Person p) {
    printf("姓名:%s\n", p.name);
    printf("年龄:%d\n", p.age);
    printf("身高:%.1f\n", p.height);
    printf("地址:%s\n", p.address);
}

void updatePerson(struct Person *p) {
    printf("请输入新的信息:\n");
    printf("姓名:");
    scanf("%s", p->name);
    printf("年龄:");
    scanf("%d", &p->age);
    printf("身高:");
    scanf("%f", &p->height);
    printf("地址:");
    scanf("%s", p->address);
}

void printEmployee(struct Employee e) {
    printf("员工信息:\n");
    printf("姓名:%s\n", e.person.name);
    printf("年龄:%d\n", e.person.age);
    printf("身高:%.1f\n", e.person.height);
    printf("地址:%s\n", e.person.address);
    printf("入职日期:%d-%d-%d\n", e.hireDate.year, e.hireDate.month, e.hireDate.day);
    printf("工资:%.2f\n", e.salary);
    printf("部门:%s\n", e.department);
}

struct Employee* createEmployee() {
    return (struct Employee*)malloc(sizeof(struct Employee));
}

void freeEmployee(struct Employee *e) {
    free(e);
}

void sortEmployees(struct Employee arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j].person.age > arr[j + 1].person.age) {
                struct Employee temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

void searchEmployee(struct Employee arr[], int size, const char *name) {
    for (int i = 0; i < size; i++) {
        if (strcmp(arr[i].person.name, name) == 0) {
            printf("找到员工:\n");
            printEmployee(arr[i]);
            return;
        }
    }
    printf("未找到员工:%s\n", name);
}