C語言--用地址調用函數

01 #include <stdio.h>
02   
03 typedef unsigned short  UNSIGNED16;
04 typedef unsigned char   UNSIGNED8;
05 typedef UNSIGNED16 (*DEMO_CYCLE)(void);
06   
07 static UNSIGNED16 lp_demo(void);
08 static UNSIGNED16 ap_demo(void);
09 static UNSIGNED16 md_demo(void);
10 static UNSIGNED16 mixed_demo(void);
11   
12 DEMO_CYCLE  p_demo_cycle = NULL;
13 DEMO_CYCLE  p_demo_cycle_group[4] = {lp_demo, ap_demo, md_demo, mixed_demo};
14 UNSIGNED8   demo_type;
15   
16 static UNSIGNED16 lp_demo(void)
17 {
18     printf("lp_demo執行這裏\n");
19     return 0;
20 }
21 static UNSIGNED16 ap_demo(void)
22 {
23     printf("ap_demo執行這裏\n");
24     return 0;
25 }
26 static UNSIGNED16 md_demo(void)
27 {
28     printf("md_demo執行這裏\n");
29     return 0;
30 }
31 static UNSIGNED16 mixed_demo(void)
32 {
33     printf("mixed_demo執行這裏\n");
34     return 0;
35 }
36   
37 int main(int argc, char *argv[])
38 {
39     UNSIGNED16  result = 0;
40     printf("%d\n",demo_type);
41         //獲取函數地址
42     p_demo_cycle = p_demo_cycle_group[demo_type];
43         //利用函數地址調用函數
44         result = (*p_demo_cycle)();
45     return 0;
46 }

#include <stdio.h>

typedef unsigned short	UNSIGNED16;
typedef unsigned char   UNSIGNED8;
typedef UNSIGNED16 (*DEMO_CYCLE)(void);

static UNSIGNED16 lp_demo(void);
static UNSIGNED16 ap_demo(void);
static UNSIGNED16 md_demo(void);
static UNSIGNED16 mixed_demo(void);

DEMO_CYCLE 	p_demo_cycle = NULL;
DEMO_CYCLE 	p_demo_cycle_group[4] = {lp_demo, ap_demo, md_demo, mixed_demo};
UNSIGNED8 	demo_type;

static UNSIGNED16 lp_demo(void)
{
	printf("lp_demo執行這裏\n");
	return 0;
}
static UNSIGNED16 ap_demo(void)
{
	printf("ap_demo執行這裏\n");
	return 0;
}
static UNSIGNED16 md_demo(void)
{
	printf("md_demo執行這裏\n");
	return 0;
}
static UNSIGNED16 mixed_demo(void)
{
	printf("mixed_demo執行這裏\n");
	return 0;
}

int main(int argc, char *argv[])
{
	UNSIGNED16  result = 0;
	printf("%d\n",demo_type);
        //獲取函數地址
	p_demo_cycle = p_demo_cycle_group[demo_type];
        //利用函數地址調用函數
		result = (*p_demo_cycle)();
	return 0;
}
函數名字符串指針:利用函數地址調用函數.同時利用數組的形式.以更加方便的形式進行操做
相關文章
相關標籤/搜索