c语言函数指针使用例子

一、是什么?

c语言函数名是一段代码首地址,连接器链接时放在text段,下面例子会把函数名打印出来,.map文件内存分布查看相关代码段函数:

下面例子实现步骤: 来源于uboot 的初始化 board_f.c
typedef int (*init_fun_t)(void);
(1)构建gd数据类型
(2)初始化全局gd变量
(3)实现指针函数内容
(4)循环遍历初始化


# 二、使用步骤
## 1.引入库
代码如下(示例):

```c
/*
 *  Definitions for Command Processor
 */
#ifndef __COMMAND_H
#define __COMMAND_H

typedef struct global_data gd_t;
typedef unsigned long phys_addr_t;
typedef unsigned long phys_size_t;

struct arch_global_data
{
	unsigned long dcache_line_size;
	unsigned long icache_line_size;
	unsigned long dcache_size;
	unsigned long icache_size;
	unsigned long reset_addr;
	unsigned long exception_addr;
	int has_initda;
	int has_mmu;
	int io_region_base;
	int mem_region_base;
	int physaddr_mask;
};

struct bd_info 
{
	unsigned long bi_flash_start;
	unsigned long bi_flashsize;
	unsigned long bi_flashoffset;
	unsigned long bi_sramstart;
	unsigned long bi_sramsize;
	unsigned long bi_arm_freq;
	unsigned long bi_dsp_freq;
	unsigned long bi_addr_freq;
	unsigned long bi_bootflags;
	unsigned long bi_ip_addr;
	unsigned long bi_ethspeed;
	unsigned long bi_intfreq;
	unsigned long bi_bus_freq;
	unsigned long bi_arch_number;
	unsigned long bi_boot_params;
	struct 
	{
       phys_addr_t start;
	   phys_size_t size;
	}bi_dram[4];

};


struct global_data 
{
    struct bd_info bd;
	unsigned long flags;
	unsigned int baudrate;
	unsigned long cpu_clk;
	unsigned long bus_clk;
	unsigned long pci_clk;
	unsigned long mem_clk;
	unsigned long has_console;
	unsigned long env_addr;
	unsigned long env_valid;
	unsigned long env_has_init;
	int env_load_prio;
	unsigned long ram_base;
	phys_addr_t ram_top;
	unsigned long relocaddr;
	phys_size_t ram_size;
	unsigned long mon_len;
	unsigned long irq_sp;
	unsigned long start_addr_sp;
	unsigned long reloc_off;
	struct global_data*new_gd;
	const void*fdt_blob;
	void*new_fdt;
	unsigned long fdt_size;
	char env_buf[32];
	unsigned int timebase_h;
	unsigned int timebase_l;
	int log_drop_count;
	int default_log_level;
	int long_fmt;
	int logc_prev;
	int log_cout;
    struct arch_global_data arch;

};

typedef int (*init_fun_t)(void);
#endif	/* __COMMAND_H */

#include 
#include 
#include "command.h"

static gd_t gd_board;

static int setup_mon_len(void)
{
    gd_board.mon_len  = 0;
	printf("setup_mon_len\n");
    return 0;
}

int arch_cpu_init(void)
{
	return 0;
}

int match_cpu_init(void)
{
	return 0;
}

static int display_new_sp(void)
{
   printf("New Stack Pointer is %081x\n",gd_board.start_addr_sp);
   return 0;
}

static int arch_setup_bdinfo(void)
{
	
	struct bd_info*bd = &(gd_board.bd);
	bd->bi_arch_number = 0x30;
  
   return 0;
}

int setup_bdinfo(void)
{
	struct bd_info*bd = &(gd_board.bd);

	arch_setup_bdinfo();
}

static int initf_bootstage(void)
{
   return 0;
}

static int jum_to_copy(void)
{
   return 0;
}

static int setup_reloc(void)
{
   return 0;
}

static int clear_bss(void)
{
	return 0;
}

#define _string1fy_1(x...) #x 


#define CONFIG_BOOTARGS  "mtd_part 0"
#define CONFIG_BOOTCOMMAND						\
	"bootp; "							\
	"setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} "\
	"ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; "\
	"bootm"


#define CONFIG_BOODELAY  5 
#define  CONFIG_BAUDRATE  115200

char default_environment[] = 
{
  "bootcmd="CONFIG_BOOTCOMMAND "\0"
  "bootargs="CONFIG_BOOTARGS     "\0"
  "bootdelay="_string1fy_1(CONFIG_BOODELAY) "\0"
};

int env_init(void)
{

	gd_board.env_addr = (unsigned long)&default_environment[0];
	gd_board.env_valid = 0;
	return 0;
}


static  init_fun_t init_sequence_f[]  = 
{
	setup_mon_len,
	arch_cpu_init,
	match_cpu_init,
	setup_bdinfo,
	initf_bootstage,
	jum_to_copy,
	setup_reloc,
	clear_bss,
	env_init,    
	display_new_sp,
};

static void hang(void)
{
   printf("#####ERROR####Please RESET the board#######\n");
   for(;;);
}

static int initcall_run_list(const init_fun_t init_sequence[])
{
   const init_fun_t*init_fnc_ptr;
   int ret = -1;

   for(init_fnc_ptr=init_sequence;*init_fnc_ptr;++init_fnc_ptr)
   {
        printf("initcall 函数名 %p\r\n",init_fnc_ptr);
   		ret = (*init_fnc_ptr)();
		if(ret)
		{
		     printf("init call sequece failed at call %p%d\n",(char*)*init_fnc_ptr,ret);
             return -1;
		}
   }

   return  0;
}


void board_init_f(void)
{
	if(initcall_run_list(init_sequence_f))
	{
		hang();
	}

}


int main()
{
	board_init_f();
	return 0;
}

gcc -I./ command.c -Wl,-Map=command.map -o command

c语言函数指针使用例子_第1张图片
text段
c语言函数指针使用例子_第2张图片

你可能感兴趣的:(#,c语言,开发语言)