第一种: 也是最常用的一种,用下标去遍历,用数组元素个数来截至。
第二种: 不常用,但是也可以。就是在数组的有效元素末尾放一个标志,依次遍历到标准处即可截至(有点类似字符串的思路)。
例如:以下为UBOOT源码
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
if ((*init_fnc_ptr)() != 0) {
hang ();
}
}
init_fnc_ptr
为init_fnc_t
类型的二重指针,init_sequence
为函数指针数组,类型相同。
*init_fnc_ptr
为条件判断,++init_fnc_ptr
指针加1,当遍历到数组中*init_fnc_ptr = NULL
时跳出循环。
*init_fnc_ptr)() != 0
执行这个函数,并判断返回值是否为0,不为零则执行hang ()
这个函数。
init_sequence
的定义
typedef int (init_fnc_t)(void);
init_fnc_t *init_sequence[] = {
cpu_init, /* basic cpu dependent setup */
#if defined(CONFIG_SKIP_RELOCATE_UBOOT)
reloc_init, /* Set the relocation done flag, must
do this AFTER cpu_init(), but as soon
as possible */
#endif
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
#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
init_func_i2c,
#endif
dram_init, /* configure available RAM banks */
display_dram_config,
NULL,
};
init_fnc_ptr
的定义
typedef int (init_fnc_t) (void);
init_fnc_t **init_fnc_ptr;