// 1. 基本聲明:返回 int,接受兩個 int 參數的函數指針
int (*pfun)(int, int); // 推薦:參數名可省略
int (*pfun)(int x, int y); // 含參數名 (僅作文檔)
// 2. 使用 typedef 簡化 (強烈推薦)
typedef int (*BinaryOp)(int, int); // 定義類型別名 BinaryOp
BinaryOp pf; // 變數聲明清晰易讀
口訣:從變數名 `pfun` 向外讀:`*` → 指針 → `()` → 函數 → 參數列表 → 返回類型。
#include
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int div(int a, int b) { return b ? a / b : 0; }
int main() {
// 1. 單指針變數賦值
int (*pf)(int, int) = add;
printf("9 + 3 = %d\n", pf(9, 3)); // 12
pf = sub; printf("9 - 3 = %d\n", pf(9, 3)); // 6
pf = mul; printf("9 * 3 = %d\n", pf(9, 3)); // 27
pf = div; printf("9 / 3 = %d\n", pf(9, 3)); // 3
// 2. 函數指針陣列:跳躍表 / 命令分發
int (*ops[4])(int, int) = {add, sub, mul, div};
char *names[] = {"加", "減", "乘", "除"};
for (int i = 0; i < 4; i++)
printf("%s: %d\n", names[i], ops[i](9, 3));
return 0;
}
將函數指針作參數傳遞,實現「控制反轉」。
// 通用數學運算執行器
int execute_op(int (*op)(int, int), int x, int y) {
return op(x, y); // 回調
}
// 排序比較函數 (qsort 模式)
int cmp_int_asc(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int cmp_int_desc(const void *a, const void *b) {
return (*(int*)b - *(int*)a);
}
void sort_demo(int *arr, int n, int (*cmp)(const void*, const void*)) {
qsort(arr, n, sizeof(int), cmp); // 標準庫回調
}
每個狀態對應一個處理函數,指針指向當前狀態處理器。
typedef enum { STATE_IDLE, STATE_RUN, STATE_STOP } State;
typedef void (*StateHandler)(void);
void idle_handler() { puts("IDLE: Waiting..."); }
void run_handler() { puts("RUN: Processing..."); }
void stop_handler() { puts("STOP: Halted."); }
StateHandler handlers[] = {idle_handler, run_handler, stop_handler};
State current = STATE_IDLE;
void transition(State next) {
current = next;
handlers[current](); // 直接調用對應處理器
}
typedef struct {
int (*draw)(void *self);
int (*area)(void *self);
// 數據成員...
} ShapeVTable;
typedef struct { ShapeVTable *vptr; int x, y; } Shape;
typedef struct { Shape base; int r; } Circle;
typedef struct { Shape base; int w, h; } Rect;
// 虛函數表實例
ShapeVTable circle_vtbl = {circle_draw, circle_area};
ShapeVTable rect_vtbl = {rect_draw, rect_area};
void circle_init(Circle *c, int x, int y, int r) {
c->base.vptr = &circle_vtbl; c->base.x=x; c->base.y=y; c->r=r;
}
// 調用: shape->vptr->draw(shape); // 動態分發
函數指針是 C 語言實現**動態行為**、**模組解耦**、**泛型演算法**的核心機制。掌握 `typedef` 簡化聲明、陣列構建跳躍表、參數傳遞實現回調,是進階 C 程式設計師的必備技能。