C語言關於指標函數與函數指標個人理解

來源:互聯網
上載者:User

標籤:use   返回   class   傳回值   指標   isp   print   hid   lap   

1,函數指標

  顧名思義,即指向函數的指標,功能與其他指標相同,該指標變數儲存的是所指向函數的地址。

假如是void類型函數指標定義方式可以是 void (*f)(參數列表);亦可以先用 typedef void (*F)(參數列表),

F f.但要注意函數指標的類型要與函數類型相同.

  下面舉個簡單的例子

 1 #include<stdio.h> 2 #include<stdlib.h> 3 typedef void (*F)();     4  5 void print_hello()          //簡單定義一個無參數的列印函數 6 { 7     printf("hello ~\n"); 8 } 9 10 void main()11 {12     F f1;                //此處相當於 void (*f1)();13     void (*f2)();14 15     f1 = print_hello;16     f2 = print_hello;17 18     f1();19     f2();20 21     system("pause");22 }
View Code

2,指標函數

  指標函數即傳回值為指標的函數。例如 int *a()、char *b()...分別代表傳回值為整形指標,字元型指標的函數。

  下面舉個簡單的例子

#include<stdio.h>#include<stdlib.h>char *str()            //定義一個傳回值為字元指標的函數{    char *a = "hello world ~";    return a;}void main(){    char *c = str();  //可以在定義的時候同時賦值    char *b;        //也可以先定義,後賦值    b = str();    printf("%s\n%s\n",c,b);    system("pause");}
View Code

 

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.