定義返回函數指針的函數
博學,切問,近思--詹子知 (https://jameszhan.github.io)
1. 基礎知識:
1) 定義函數指針:
return_type (*func_pointer)(parameter_list)
2) 定義返回函數指針的函數:
return_type(*function(func_parameter_list))(parameter_list)
定義了一個函數function,該函數的參數列表是(function_patameter_list),返回類型是一個函數指針,這個函數指針的原型是return_type(*)(parameter_list)。
2. 經典例子[signal函數原型]
Linux 2.0之前版本
void (*signal (int signo,
void (*func)(int))) (int);
Linux 2.6 版本
typedef void (*__sighandler_t) (int);
extern __sighandler_t signal (int __sig, __sighandler_t __handler)
3. Example
可以用以下兩種方式定義返回函數指針的函數。第二種方式是第一種方式的替換, 也更易理解。
1). int (*OP(char))(int,
int);
2). typedef int OP(int,
int);
OP* fun(char c);
#include<stdio.h>
int (*opp(char))(int,
int);
typedef int OP(int,
int);
OP* fun(char c);
int add(int a,
int b){
return (a + b);
}
int product(int a,
int b){
return (a * b);
}
int main(void){
int a =
2, b = 23;
printf("Hello World:%d, %d/n", opp('+')(a, b), opp('*')(a, b));
printf("Hello World:%d, %d/n", fun('+')(a, b), fun('*')(a, b));
return 0;
}
int (*opp(char c))(int a,
int b){
if(c ==
'+'){
return add;
}else{
return product;
}
}
OP* fun(char c){
if(c ==
'+'){
return add;
}else{
return product;
}
}
最後更新:2017-04-02 04:00:25
上一篇:
Oracle數據安全解決方案(3)——Oracle Audit Vault
下一篇:
How to execute shell script in Java?
《數據驅動安全:數據安全分析、可視化和儀表盤》一第3章
FlashMax安裝驅動
4月21日雲棲精選夜讀:【校園頭條】第1期:找實習、找工作時,讓你脫穎而出的秘籍
穀歌美女副總裁為佩奇管理層重組最大輸家
解決:<net.sf.ehcache.util.UpdateChecker> : New update(s) found: 2.6.5
Word2010中搜狗拚音輸入法(各種輸入法)消失了的解決辦法
Kafka集群磁盤使用率瞬超85%,幕後元凶竟是它?
一篇文章搞懂人工智能、機器學習和深度學習之間的區別
SQL語句的基本操作
Facebook、中國BAT均落選,高盛全球“漂亮50”有哪些潛力股?