# C语言函数指针的用法

int f(int);
int (*fp)(int) = &f;

//使用三种方式调用函数
int ans;
ans = f(25);
ans = (*pf)(25);
ans = pf(25);

#include <stdio.h>

int max(int x, int y)
{
return x > y ? x : y;
}

int main(void)
{
/* p 是函数指针 */
int (* p)(int, int) = & max; // &可以省略
int a, b, c, d;

printf("please input 3 numbers:");
scanf("%d %d %d", & a, & b, & c);

/* 与直接调用函数等价，d = max(max(a, b), c) */
d = (* p)(( *p)(a, b), c);

printf("the maxumum number is: %d\n", d);
return 0;
}

## 1、回调函数：

Node* search_list(Node* node, int const value)
{
while(node != NULL) {
if(node->value == value)
break;
node = node->next;
}
return node;
}

#include <stdio.h>
#include "node.h"

Node *search_list( Node *node, void const *value,
int (*compare)( void const *, void const * ) )
{
while( node != NULL ){
if( compare( &node->value, value ) == 0 )
break;
node = node->next;
}
return node;
}

int compare_ints(void const *a, void const *b)
{
if( *(int *)a == *(int *)b)
return 0;
else
return 1;
}

//这个函数将像下面这样调用
desired_node = search_list(root, &desired_value, compare_ints);

#include<string.h>
...
desired_node = search_list(root, "desired_value", strcmp);

#include<stdio.h>
struct object
{
int data;
};

int object_compare(struct object * a,struct object * z)
{
return a->data < z->data ? 1 : 0;
}

struct object *maximum(struct object * begin,struct object * end,int (* compare)(struct object *, struct object *))
{
struct object * result = begin;
while(begin != end)
{
if(compare(result, begin))
{
result = begin;
}
++ begin;
}
return result;
}

int main(void)
{
struct object data[8] = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}};
struct object * max;
max = maximum(data + 0, data + 8, & object_compare);
printf("max: %d\n", (*max).data);
return 0;
}

## 2、转移表

下面的程序是一个简化的根据运算符转到相应运算的例子：

#include<stdio.h>

double _sub(double, double);
double _mul(double, double);
double _div(double, double);

double _add(double a, double b)
{
return a + b;
}

double _sub(double a, double b)
{
return a - b;
}

double _mul(double a, double b)
{
return a * b;
}

double _div(double a, double b)
{
return a / b;
}

int main(void)
{
int n;
enum Operation{ADD, SUB, MUL, DIV}op;
double a, b, ans;
a = 0.232332;
b = 0.234398;
printf("请输入一个整数(0-3): ");
scanf("%d", &n);
op = (enum Operation)n;
switch(op) {
ans = _add(a, b);
break;
case SUB:
ans = _sub(a, b);
break;
case MUL:
ans = _mul(a, b);
break;
case DIV:
ans = _div(a, b);
break;
default:
break;
}
printf("%lf\n", ans);
return 0;
}

#include<stdio.h>

double _sub(double, double);
double _mul(double, double);
double _div(double, double);

double _add(double a, double b)
{
return a + b;
}

double _sub(double a, double b)
{
return a - b;
}

double _mul(double a, double b)
{
return a * b;
}

double _div(double a, double b)
{
return a / b;
}

int main(void)
{
int n;    double a, b, ans;
a = 0.232332;
b = 0.234398;
printf("请输入一个整数(0-3): ");
scanf("%d", &n);

double (*oper_func[])(double, double) = {
_add, _sub, _mul, _div
};

ans = oper_func[n](a, b);
printf("%lf\n", ans);
return 0;
}

+ 订阅