函式指標例子說明

2021-06-26 22:21:15 字數 4821 閱讀 5136

在c/c++中存在著函式指標,即指向函式的指標。我目前已知的兩種使用方法是:

[cpp]view plain

copy

#include 

#include 

typedef

int* pinnt;  

#define pp int*

intfunca(

inta,

intb);  

intfuncb(

int* a,

int*b);  

intmain(

intargc, 

char

*argv)  

intfunca(

inta,

intb)  

intfuncb(

int* a,

int*b)    

這裡int (*func)(int,int)定義了乙個函式指標變數,可以接受函式名或者函式名的指標(這裡我還沒有很清楚)。

然後我在csdn上面找到了乙個例子,據說是華為的面試題目,**如下,這時程式正常輸出值為110。

[cpp]view plain

copy

#include 

intinc(

inta)   

intmulti(

int*a,

int*b,

int*c)   

typedef

int(func1)(

int);   

typedef

int(func2)(

int*,

int*,

int*);   

void

show(func2   fun,

intarg1,   

int*arg2)   

main()     

一開始我沒有理解這個用法的時候,以為這裡使用的方法與方法一相同,故直接寫成

[cpp]view plain

copy

typedef

int(func1)(

int);   

typedef

int(func2)(

int*,

int*,

int*);   

void

show(func2   fun,

intarg1,   

int*arg2)   

這時候,編譯器給出的提示是:

1>d:\vctest\test4\test4\test4.c(17) : error c2513: 'int (int)' : no variable declared before '=' 

func1 = inc這個語句的左邊沒有變數被申明,這個錯誤提示說明利一點:func是一種型別而不是乙個變數。做出如下更正:

[html]view plain

copy

func1  *p= 

inc;   

程式正確,更加證明了

[cpp]view plain

copy

typedef

int(func1)(

int);   

typedef

int(func2)(

int*,

int*,

int*);   

語句定義了兩個自己的資料型別func1和func2,這兩個型別申明的變數用儲存函式指標

也可以採用下面的方式進行定義:

[cpp]view plain

copy

typedef

int(*func1)(

int);   

typedef

int(func2)(

int*,

int*,

int*);   

void

show(func2   fun,

intarg1,   

int*arg2)   

以上例子,涉及到的概念有函式指標,函式型別和函式指標變數

函式指標變數有兩種實現方法:

1. 先申明函式指標型別,在用函式指標型別申明函式指標變數

//typedef void funtype(int);  -- funtype:同樣是乙個函式指標型別。

[cpp]view plain

copy

typedef

void

(*pf)(

int,

int);  

//使用函式指標型別申明的變數就是函式指標變數

//pf a = &inc//pf a = inc

2. 直接定義乙個函式指標變數

[cpp]view plain

copy

void

(*fpv)(

int,

int);  

fvp  = inc // fvp = &inc

函式型別與函式指標變數

如何定義函式型別呢

[cpp]view plain

copy

typedef

void

(ft)(

int,

int);  

申明的函式型別如何使用呢?

首先可以用來申明函式:

ft x,y等價於:

void x(int,int);

void y(int,int);

請看如下的示例:

[cpp]view plain

copy

#include 

typedef

int(ft)(

int*,

int*);  

ft x,y;  

main()   

intx(int

* a,

int*b)  

inty(

int*a,

int* b)  

上面的程式輸出3和6

[cpp]view plain

copy

#include 

typedef

int(ft)(

int*,

int*);  

ft x,y,*z,u;  

main()   

intx(int

* a,

int*b)  

inty(

int*a,

int* b)  

上面的例子中ft *z其實是申明了乙個指向函式的函式指標變數,類似於int(*z)(int*,int*)。

最後再來看下那個困擾了我很久的例子吧:

[cpp]view plain

copy

#include 

intinc(

inta)   

intmulti(

int*a,

int*b,

int*c)   

typedef

int(*func1)(

int);   

typedef

int(func2)(

int*,

int*,

int*);   

typedef

int(func3)(

int*,

int*,

int*);   

void

show(func2 *fun,

intarg1,   

int*arg2)   

//void   show(func2 fun,int   arg1,   int*arg2)//這裡做這樣的宣告也一樣是對的 

main()     

最讓我疑惑的是

void   show(func2 *fun,int   arg1,   int*arg2) 

//void   show(func2 fun,int   arg1,   int*arg2)//這裡做這樣的宣告也一樣是對的

這兩種宣告方式都是對的,採用void   show(func2 fun,int   arg1,   int*arg2)//這裡做這樣的宣告也一樣是對的這種方式宣告, show(multi,1,&a);呼叫時正確的,

而前面的**中,我也說過//u = x;//這裡出錯了這裡u是乙個函式名稱,是乙個指標常量,類似於陣列名稱,是不能夠被賦值的, 這個問題困擾我很久啊,仔細考慮之後,我個人的觀點是:void   show(func2 fun,int   arg1,   int*arg2)使用乙個函式做為形式引數,呼叫時傳遞乙個函式名字給他就可以,也可以傳遞乙個函式常量指標。模擬於使用指標常量或者陣列作函式書引數的時候

[cpp]view plain

copy

#include 

typedef

void

(ft)(

int* 

const

a);  

ft z;  

void

main()  

;  int

b = 5;  

int*c = &b;  

z(a);  

//但是如果在這裡給a = c是不對的

}  void

z(int

* const

a)    

指標函式與函式指標 兩個簡單例子進行說明

一直都對兩個概念有所混淆 指標函式與函式指標,下面我們通過兩個例子來講解一下 開始1 指標函式 1 基本概念 指標函式 顧名思義就是帶有指標的函式,即其本質是乙個函式,只不過這種函式返回的是乙個對應型別的位址。2 定義式 type func type type 如 int max int x,int...

指標作為函式的出入引數例子說明

1 第一種 引數是入參也是出參 函式裡面的 pmon 是指標指向的值 得到指定月份的上乙個月份 void get prev month int32 pyear,int32 pmon if pmon 1 pyear 1 pmon 12 else pmon 1 2 字串轉換成字元陣列 輸入 src指標 ...

c 函式指標說明

下面隨筆說明函式指標用法。定義形式 儲存型別 資料型別 函式指標名 含義 函式指標指向的是程式 儲存區 通過函式指標呼叫的函式 例如將函式的指標作為引數傳遞給乙個函式,使得在處理相似事件的時候可以靈活的使用不同的方法。呼叫者不關心誰是呼叫者 需知道存在乙個具有特定原型和限制條件的被呼叫函式。1 in...