C語言 不曾知曉 獲取函數地址,利用這個地址調用函數

#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;
}

函數名字符串指針:利用函數地址調用函數.同時利用數組的形式.以更加方便的形式進行操做
相關文章
相關標籤/搜索