typedef int (init_fnc_t) (void);
init_fnc_t *init_sequence[] = {
cpu_init, /* basic cpu dependent setup */
board_init, /* basic board dependent setup */
interrupt_init, /* set up exceptions */
env_init, /* initialize environment */
init_baudrate, /* initialze baudrate settings */
serial_init, /* serial communications setup */
console_init_f, /* stage 1 init of console */
display_banner, /* say that we are here */
#if defined(CONFIG_DISPLAY_CPUINFO)
print_cpuinfo, /* display cpu info (and speed) */
#endif
#if defined(CONFIG_DISPLAY_BOARDINFO)
checkboard, /* display board info */
#endif
dram_init, /* configure available RAM banks */
display_dram_config,
NULL,
};
void start_armboot (void)
{
init_fnc_t **init_fnc_ptr;
.........
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr)
{
if ((*init_fnc_ptr)() != 0)
{
hang ();
}
}
}
理解:1) init_fnc_t声明为函数类型,这个函数类型的参数为空,函数返回值为int整型。
2)init_fnc_t *init_sequence[] 因为init_fnc_t 已经声明为函数的类型,所以init_fnc_t *init_sequence[] 的意思是: 定义了一个函数指针数组,init_sequence为函数指针数组名,其成员变量是指向函数的指针变量(指向函数的类型是:参数为空,函数返回值为int整型)
3):
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr)
{
if ((*init_fnc_ptr)() != 0)
{
hang ();
}
}
为调用函数指针数组中成员变量所指向的函数,如果调用成功的话,被调用的函数返回0,调用不成功,程序被挂起。
附:
typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)
C/C++的知识补充 2011-03-18 21:35:53 阅读14 评论0 字号:大中小 订阅
1、typedef int (init_fnc_t) (void);表示定义init_fnc_t为函数类型,该函数返回int型,无参数。
而“init_fnc_t *init_sequence[]={ cpu_init, board_init }”表示用init_fnc_t(函数类型)去定义一个一维指针数组,数组中的元素都是指针变量,而且都是指向函数的指针,这些函数返回值都是int型,无参数的。更明朗的说就是数组中的每个元素是用来存放函数入口首地址的。
2、int (*init_fnc_t) (void);表示定义一个函数指针(返回值为int型,无参数的函数指针,存放的是函数的首地址),typedef int (*init_fnc_t) (void);表示定义一个函数指针类型,即可以直接用init_fnc_t这个符号去定义函数指针,“init_fnc_t init_sequence[]={cpu_init, board_init }”表示用init_fnc_t(函数指针类型)去定义一个数组,数组里面的元素都是一个函数指针,cpu_init, board_init 都是函数指针,存放的都是函数的首地址。
综上的分析,我个人觉得这两个定义的意思是一样的只是写法不一样而已,1中的数组元素是指向函数的指针变量,2中的数组的元素是函数指针。因此得到的结果是一样的。
说实话,typedef的用法真的挺变态,一部小心会让你掉入陷阱!!!不过能够用好typedef并深刻理解他的内涵,我觉得个人的C语言能力会是一个大提高!