C/C++ 指標函數 與 函數指標

來源:互聯網
上載者:User

標籤:return   pre   bsp   調用   ret   變數   通過   div   傳回值   

指標函數是個函數,是返回指標類型到函數。

函數指標是個指標,是指向函數地址到指標。

區分兩者的關鍵點是,函數名是否用*和括弧“包圍”起來。

 1 #include <stdio.h> 2 int *create_node(int);  //指標函式宣告 3 int *(*fun)(int);   //函數指標聲明, 指向返回int*且有一個int參數的函數. 4  5 //定義一個指標函數(傳回值是指標的函數) 6 int *create_node(int val){ 7     int *a = new int; 8     *a = val; 9     return a;10 }11 12 int main (){ 13     //通過函數名稱調用函數, 這個函數名稱就是函數的指標14     //函數名稱指向就是函數到地址15     int *a = create_node(4);16     printf("a_val : %d\n", *a);17     delete a;18 19     //函數指標是個指標變數20     fun = create_node;21     int *b = fun(5);22     printf("b_val : %d\n", *b);23     delete b;24 25     return 0;   26 }

 

 就簡單說明到這裡吧, 看代碼注釋。^_^

C/C++ 指標函數 與 函數指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.