函数指针是一个非常强大的编程工具,可以让我们以更灵活的方式编写程序。 在这篇文章中,我们将介绍函数指针的6个高级应用场景,并贴出相应的代码案例和解释。
本文引用地址:
打回来
回调函数是事件发生时调用的函数。 通常,回调函数被注册在某个库函数或者框架函数中,当满足某个条件时,库函数或者框架函数就会调用回调函数来执行相应的操作。 这是一个例子:
#include
void handle_event(int event_type, void (*callback)(void))
{
printf("event %d occurredn", event_type);
if (callback)
{
callback();
}
}
void callback_function()
{
printf("callback function calledn");
}
int main()
{
handle_event(1, callback_function);
handle_event(2, NULL);
return
0;
}
在上面的代码中,我们定义了一个带有两个参数的函数:事件类型和函数指针。 如果函数指针不为空,则调用指定的函数。
在main函数中,我们调用函数分别触发两个事件,其中第一个事件注册了回调函数,第二个事件没有注册回调函数。
函数参数化
函数参数化是指通过函数指针对函数的某些行为进行参数化。 这样,我们就可以动态地指定函数被调用时的行为。 这是一个例子:
#include
void process_array(int *array, size_t size, int (*process)(int))
{
for (size_t i = 0; i < size; i++)
{
array[i] = process(array[i]);
}
}
int increment(int n)
{
return n + 1;
}
int main()
{
int array[] = {1, 2, 3, 4, 5};
size_t size = sizeof(array) / sizeof(int);
process_array(array, size, increment);
for (size_t i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("n");
return 0;
}
在上面的代码中,我们定义了一个带有三个参数的函数:一个整数数组、数组的大小和一个函数指针。 函数指针指向一个接受整数参数并返回整数结果的函数。
在函数中,我们将数组中的每个元素传递给指定的函数,然后将函数的返回值存储回原始数组中。
在main函数中,我们定义了一个函数,将传入的整数加1。然后,我们调用该函数来处理整数数组并打印出结果。
排序算法
排序算法是函数指针的另一个常见用例。 通过传递不同的比较函数,我们可以在不同的排序算法中重用相同的代码。 这是一个例子:
#include
#include
typedef int (*compare_func_t)(const void *, const void *);
void sort(int *array, size_t size, compare_func_t compare_func)
{
qsort(array, size, sizeof(int), compare_func);
}
int compare_int(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}
int compare_reverse_int(const void *a, const void *b)
{
return (*(int*)b - *(int*)a);
}
int main()
{
int array[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
size_t size = sizeof(array) / sizeof(int);
sort(array, size, compare_int);
for (size_t i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("n");
sort(array, size, compare_reverse_int);
for (size_t i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("n");
return 0;
}
在上面的代码中,我们定义了一个排序函数,它接受三个参数:一个整数数组、数组的大小和一个比较函数指针。
比较函数指针指向一个函数,该函数接受两个指向常量 void 类型的指针并返回一个整数结果。
在排序函数中,我们使用标准库函数qsort对整数数组进行排序,比较函数指针由调用者传递。
在主函数中,我们定义了两个比较函数 和 ,分别用于升序和降序排序。 然后,我们调用 sort 函数对整数数组进行排序并打印结果。
函数指针数组
函数指针数组是其中每个元素都是函数指针的数组。 这种数组可以用来实现一个调度表,根据不同的输入参数动态调用不同的函数。 这是一个例子:
#include
void add(int a, int b)
{
printf("%d + %d = %dn", a, b, a + b);
}
void subtract(int a, int b)
{
printf("%d - %d = %dn", a, b, a - b);
}
void multiply(int a, int b)
{
printf("%d * %d = %dn", a, b, a * b);
}
void divide(int a, int b)
{
if (b == 0)
{
printf("cannot divide by zeron");
}
else
{
printf("%d / %d = %dn", a, b, a / b);
}
}
typedef void (*operation_func_t)(int, int);
int main()
{
operation_func_t operations[] = {add, subtract, multiply, divide};
size_t num_operations = sizeof(operations) / sizeof(operation_func_t);
int a = 10, b = 5;
for (size_t i = 0; i < num_operations;i++)
{
operations[i](a,b);
}
return 0;
}
在上面的代码中,我们定义了四个函数 add、 、 和 分别对两个整数进行加法、减法、乘法和除法运算。
然后,我们定义一个函数指针类型,它指向一个带有两个整数参数且不返回任何值的函数。
接下来,我们定义一个函数指针数组,其中每个元素都是一种函数指针类型,分别指向 add、 、 和。
在main函数中,我们使用for循环来遍历数组,依次调用每个函数指针指向的函数。 在每次函数调用之前,我们可以根据需要设置a和b的值。 这样,我们就可以动态选择要执行的操作。
函数指针和回溯
回溯是一种解决一些组合优化问题的算法,通常使用递归来实现。 函数指针可以用来实现回溯算法的一些关键部分。
这是使用回溯来计算排列的示例:
#include
#include
typedef void (*callback_func_t)(const int *, size_t);
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void permute(int *nums, size_t len, size_t depth, callback_func_t callback) {
if (depth == len)
{
callback(nums, len);
return;
}
for (size_t i = depth; i < len; i++)
{
swap(&nums[depth], &nums[i]);
permute(nums, len, depth + 1, callback);
swap(&nums[depth], &nums[i]);
}
}
void print_array(const int *arr, size_t len)
{
for (size_t i = 0; i < len; i++)
{
printf("%d ", arr[i]); }
printf("n");
}
}
int main()
{
int nums[] = {1, 2, 3};
permute(nums, sizeof(nums) / sizeof(int), 0, print_array);
return 0;
}
在上面的代码中,我们定义了一个计算给定数组的排列的函数。
在函数中,我们使用递归来生成所有可能的排列,并使用函数指针来指定每当生成排列时应调用的函数。
在这个例子中,我们将函数作为回调函数传递给函数。 这意味着每当函数生成排列时,它都会调用该函数来打印该排列。
在main函数中,我们定义了一个包含三个整数的数组nums,并使用该函数计算该数组的所有排列。 每次生成排列时,该函数都会调用该函数来打印排列。
函数指针和多态性
多态性是面向对象编程中的一个重要概念,它允许我们在不知道对象类型的情况下调用相应的函数。 虽然C语言不是面向对象的编程语言,但是我们仍然可以使用函数指针来实现多态性。
这是使用函数指针的多态性示例:
#include
#include
typedef struct shape
{
void (*draw)(struct shape *);
} shape_t;
typedef struct circle
{
shape_t shape;
int x;
int y;
int r;
} circle_t;
typedef struct rectangle
{
shape_t shape;
int x;
int y;
int w;
int h;
} rectangle_t;
void circle_draw(shape_t *shape)
{
circle_t *circle = (circle_t *)shape;
printf("Drawing a circle at (%d, %d) with radius %d.n", circle->x, circle->y, circle->r);
}
void rectangle_draw(shape_t *shape)
{
rectangle_t *rectangle = (rectangle_t *)shape;
printf("Drawing a rectangle at (%d, %d) with width %d and height %d.n", rectangle->x, rectangle->y, rectangle->w, rectangle->h);
}
int main()
{
circle_t circle =
{
.shape = {circle_draw},
.x = 10,
.y = 20,
.r = 5,
};
rectangle_t rectangle =
{
.shape = {rectangle_draw},
.x = 30,
.y = 40,
.w = 15,
.h = 20,
};
shape_t *shapes[] = {(shape_t *)&circle, (shape_t *)&rectangle};
for (size_t i = 0; i < sizeof(shapes) / sizeof(shape_t *); i++)
{
shapes[i]->draw(shapes[i]);
}
return 0;
}
上面的代码中,我们定义了一个shape结构体,它有一个函数指针draw,用来绘制shape。
我们还定义了两个形状: 和 ,每个形状都包含自己的属性和指向形状结构的指针。 每个形状都定义了自己的绘制函数来绘制该形状。
在 main 函数中,我们定义了一个包含 1 和 1 类型的数组。 我们使用循环来迭代这个数组,并使用每个形状的绘制函数来绘制形状。
请注意,虽然数组中的元素是*类型,但我们仍然可以调用每个元素的draw函数,因为 和 派生自 ,它们都包含一个draw函数指针。
本例演示了如何使用函数指针来实现多态性。 虽然C语言不支持面向对象编程,但我们可以使用结构体和函数指针来实现类似的概念。
总结
函数指针是一个强大的工具,可用于实现许多不同的编程模式和算法。
在本文中,我们介绍了函数指针的基本概念和语法,并提供了一些高级应用场景的代码示例,包括回调函数、函数指针数组、函数指针作为参数、函数指针与递归、函数指针与多态性等..
使用函数指针可以帮助我们编写更加灵活、通用的代码,并提高代码的可重用性和可扩展性。
原来的: