原创博客,欢迎光临
嵌入式是本职,AI 是兴趣,其它是无奈

回调函数介绍与C语言实例(基于 DeepSeek 整理 )

#重学 C 语言

什么是回调函数

回调函数(Callback Function)是一种通过函数指针调用的函数。简单来说,就是将一个函数作为参数传递给另一个函数,在特定事件或条件发生时被调用执行的函数。

回调函数的主要特点

  1. 由调用方定义,由被调用方执行
  2. 实现了调用方与被调用方的解耦
  3. 常用于事件处理、异步操作等场景

回调函数的优势

  1. 灵活性:可以在运行时决定调用哪个函数
  2. 解耦:调用方和被调用方不需要直接知道对方的具体实现
  3. 可扩展性:可以轻松添加新的回调函数而不修改原有代码

C语言实现回调函数的实例

示例1:简单的回调函数

#include <stdio.h>

// 回调函数类型定义
typedef void (*Callback)(int);

// 执行某些操作并调用回调函数
void performOperation(int value, Callback callback) {
    printf("正在执行操作,值为: %d\n", value);
    // 调用回调函数
    callback(value);
}

// 回调函数实现1
void callbackFunction1(int num) {
    printf("回调函数1被调用,接收到的值: %d\n", num * 2);
}

// 回调函数实现2
void callbackFunction2(int num) {
    printf("回调函数2被调用,接收到的值: %d\n", num * num);
}

int main() {
    // 使用回调函数1
    performOperation(5, callbackFunction1);

    // 使用回调函数2
    performOperation(5, callbackFunction2);

    return 0;
}

示例2:带上下文的回调函数

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

// 回调函数类型定义,带上下文参数
typedef void (*CallbackWithContext)(const char*, void*);

// 模拟事件处理器
void eventHandler(const char* eventName, CallbackWithContext callback, void* context) {
    printf("事件 '%s' 已触发\n", eventName);
    callback(eventName, context);
}

// 回调函数实现
void logEvent(const char* eventName, void* context) {
    char* prefix = (char*)context;
    printf("[%s] 记录事件: %s\n", prefix, eventName);
}

// 另一个回调函数实现
void countEvent(const char* eventName, void* context) {
    int* count = (int*)context;
    (*count)++;
    printf("事件计数: %d (最新事件: %s)\n", *count, eventName);
}

int main() {
    char logPrefix[] = "APP";
    int eventCount = 0;

    // 触发事件并使用不同的回调函数
    eventHandler("启动", logEvent, logPrefix);
    eventHandler("点击", countEvent, &eventCount);
    eventHandler("关闭", logEvent, logPrefix);
    eventHandler("滚动", countEvent, &eventCount);

    printf("总事件计数: %d\n", eventCount);

    return 0;
}

示例3:排序算法中的回调函数(比较函数)

#include <stdio.h>
#include <stdlib.h>

// 比较函数类型定义
typedef int (*CompareFunc)(const void*, const void*);

// 冒泡排序实现,使用回调函数进行比较
void bubbleSort(int* array, int size, CompareFunc compare) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (compare(&array[j], &array[j + 1]) > 0) {
                // 交换元素
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
}

// 升序比较函数
int compareAscending(const void* a, const void* b) {
    return (*(int*)a - *(int*)b);
}

// 降序比较函数
int compareDescending(const void* a, const void* b) {
    return (*(int*)b - *(int*)a);
}

// 打印数组
void printArray(int* array, int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");
}

int main() {
    int numbers[] = {5, 2, 9, 1, 5, 6};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    printf("原始数组: ");
    printArray(numbers, size);

    // 升序排序
    bubbleSort(numbers, size, compareAscending);
    printf("升序排序: ");
    printArray(numbers, size);

    // 降序排序
    bubbleSort(numbers, size, compareDescending);
    printf("降序排序: ");
    printArray(numbers, size);

    return 0;
}

这些示例展示了回调函数在不同场景下的应用,包括简单回调、带上下文的回调以及在算法中的应用。回调函数是C语言中实现灵活、可扩展代码的重要技术之一。

赞(1)
未经允许不得转载:程序员大狐狸 » 回调函数介绍与C语言实例(基于 DeepSeek 整理 )

评论 抢沙发

登录

找回密码

注册