函數指標和指標函數的區別

來源:互聯網
上載者:User

標籤:src   span   .com   alt   stdio.h   code   add   mes   資料類型   

1.指標函數 _type_ *function(int, int)

_type_ *function(int, int)與普通函數int function(int,int)類似,只是返回的資料類型不一樣而已,_type_ *function(int, int)返回的是指標地址,int function(int,int)返回的是int型資料。

 1 #include "stdio.h" 2 #include "stdlib.h" 3  4 int sum =0; 5 int *getAdd(int a, int b) 6 { 7     sum = a+b; 8     return ∑ 9 }10 11 int getDiff(int a, int b)12 {13     return a>b?(a-b):(b-a);14 }15 16 int main()17 {18     int *pTemp, Temp;19     pTemp = getAdd(115,10);20     printf("ADD result:%d\n", *pTemp);21     22     Temp = getDiff(115,10);23     printf("DIFF result:%d\n", Temp);24     return 0;25 }

2.函數指標__type__ (*function)(int,int)

__type__ (*function)(int,int)只是一個指標,變數指標是指向一個變數的地址,結構體指標是指向一個結構體的首地址,而函數指標是指向一個函數的地址。

 1 #include "stdio.h" 2 #include "stdlib.h" 3  4 int (*function)(int ,int); 5  6 int Max(int a, int b) 7 { 8     return a>b?a:b; 9 }10 11 int Min(int a, int b)12 {13     return a<b?a:b;14 }15 16 int main()17 {18     int Num1 = 20, Num2 = 30;19 20     function = Max;21     printf("Max is %d\n", function(Num1, Num2));22     function = Min;23     printf("Min is %d\n", function(Num1, Num2));24     return 0;25 }

函數指標和指標函數的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.