原文地址:u-boot中typedef應用解析___init_fnc_t*init_sequence[]作者:謝爭
u-boot中有這麼一段代碼。
/*這裏定義了一個新的數據類型init_fnc_t,
*這個數據類型是參數爲空,返回值爲int的函數。 */ typedef int
(init_fnc_t) (void); /*init_sequence是一個指針數組,指向的是init_fnc_t類型的函數*/
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 */ dram_init,
/* configure available RAM banks */ display_dram_config, #if defined(CONFIG_VCMA9)|| defined(CONFIG_CMC_PU2) checkboard, #endif NULL, };
/*init_fnc_ptr爲指向函數指針的指針*/
init_fnc_t **init_fnc_ptr; /*init_fnc_ptr初始化指向init_sequence指針數組,下面的循環遇到NULL結束*/ for (init_fnc_ptr= init_sequence;*init_fnc_ptr;++init_fnc_ptr)
{ if
((*init_fnc_ptr)()!=
0)
{
/*(*init_fnc_ptr)()爲C中調用指針指向的函數*/ hang
(); } }
|
自己寫了2個test程序
一個typedef int (test_fnc_t) (void);
一個typedef int (*test_fnc_t) (void);
#include<stdio.h>
int test0 (void); int test1 (void);
typedef int
(*test_fnc_t)(void);
test_fnc_t test_sequence[]=
{ test0, test1, NULL, };
//int _tmain(int argc, _TCHAR* argv[])
int main() { test_fnc_t *test_fnc_ptr;
for
(test_fnc_ptr = test_sequence;*test_fnc_ptr;++test_fnc_ptr)
{ if((*test_fnc_ptr)()!=
0)
{ printf("error here!"); } } return 0; }
int test0 (void) { printf("test0n"); return 0; }
int test1
(void) { printf("test1n"); return 0; }
|
#include<stdio.h>
int test0 (void);
int test1 (void);
typedef int
(test_fnc_t) (void);
test_fnc_t *test_sequence[]=
{
test0,
test1,
NULL,
};
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
test_fnc_t **test_fnc_ptr;
for
(test_fnc_ptr = test_sequence;*test_fnc_ptr;++test_fnc_ptr)
{
if((*test_fnc_ptr)()!=
0)
{
printf("error here!");
}
}
return 0;
}
int test0 (void)
{
printf("test0n");
return 0;
}
int test1 (void)
{
printf("test1n");
return 0;
}