c語言.函數指針數組


函數指針: 一個指向函數的指針。一般用函數名表示。

函數指針數組:元素為函數指針的數組。轉移表。c語言中函數不可以定義為數組,只能通過定義函數指針來操作。

 1 #include<stdio.h>
 2 
 3 //function statement
 4 void func(void);
 5 void func0(void);
 6 void func1(void);
 7 void func2(void);
 8 //defined function pointer array ,& assigned
 9 int(* funcArr[])(void) = { func0,func1,func2 };
10 
11 int a;
12 
13 int main()
14 {
15     func();
16     printf("main = %p\n",main);
17     //Function pointer
18     int(*pfunc)(void) = func;
19     pfunc();
20 
21     a = 2;
22     while (a) {
23         //function pointer array
24         funcArr[a]();
25     }
26     
27     system("pause");
28     return 0;
29 }
30 //function definition
31 void func(void) {
32     printf("hello wworld\n");
33     return 0;
34 }
35 void func0() {
36     printf("function0\n");
37     a--;
38     return 0;
39 }
40 void func1() {
41     printf("function1\n");
42     a--;
43     return 0;
44 }
45 void func2() {
46     printf("function2\n");
47     a--;
48     return 0;
49 }

注意:

 " [ ] "優先級高於“ * ”。

 

參考:

https://blog.csdn.net/u010925447/article/details/74295692

https://blog.csdn.net/qq_29924041/article/details/53933104

https://blog.csdn.net/u014265347/article/details/54882661


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM