深入剖析u-boot代码typedef int (init_fnc_t) (void)
2013-03-29 11:05
477 查看
今天学习u-boot源码时,看到一句定义:typedef int (init_fnc_t) (void);很久才弄明白,在此分享自己的理解,希望对你有帮助。该定义位于:u-boot-2013.01\arch\arm\lib\board.c第209行。
u-boot中的代码(有删减):
要注意typedef申明的特点。下面以两种实现来说明该定义的含义。
typedef_test1.c:
typedef_test2.c:
u-boot中的代码(有删减):
typedef int (init_fnc_t) (void); /* 这里定义了一个新的数据类型init_fnc_t,这个数据类型是参数为空,返回值为int的函数 */ init_fnc_t *init_sequence[] = { /* init_sequence是一个指针数组,指向的是init_fnc_t类型的函数 */ arch_cpu_init, /* basic arch cpu dependent setup */ mark_bootstage, 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 */ NULL, }; init_fnc_t **init_fnc_ptr; /* init_fnc_ptr为指向函数指针的指针 */ for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) { /* init_fnc_ptr初始化指向init_sequence指针数组,下面的循环遇到NULL结束 */ if ((*init_fnc_ptr)() != 0) { /* (*init_fnc_ptr)()为C中调用指针指向的函数 */ hang (); } }
要注意typedef申明的特点。下面以两种实现来说明该定义的含义。
typedef_test1.c:
/* * typedef_test1.c -- test typedef * Author: zbqwxy *//* * output: Test0 * Test1 */ #include <stdio.h> int test0(void) { printf("Test0\n"); return 0; } int test1(void) { printf("Test1\n"); return 0; } typedef int (test_fnc_t)(void); //注意此处与typedef_test2.c的不同 test_fnc_t *test_sequence[] = { test0, test1, NULL, }; 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!\n"); } } return 0; }
typedef_test2.c:
/* * typedef_test2.c -- test typedef * Author: zbqwxy *//* * output: Test0 * Test1 */ #include <stdio.h> int test0(void) { printf("Test0\n"); return 0; } int test1(void) { printf("Test1\n"); return 0; } typedef int (*test_fnc_t)(void); //注意此处与typedef_test1.c的不同 test_fnc_t test_sequence[] = { test0, test1, NULL, }; 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!\n"); } } return 0; }
相关文章推荐
- linux内核学习初笔记(3)u-boot执行第二阶段typedef int (init_fnc_t) (void)解释
- uboot中typedef int (init_fnc_t) (void);详解
- typedef int(init_fnc_t) (void) 和 typedef int(*init_fnc_t) (void);
- typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)
- 关于typedef int (init_fnc_t) (void);理解
- typedef int(init_fnc_t) (void)的解释
- typedef int (*init_fnc_t)(void);的理解
- typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)
- typedef int(init_fnc_t) (void) 和 typedef int(*init_fnc_t) (void)函数类型和函数指针的区别
- OMAPL138学习---typedef int(init_fnc_t)(void)
- typedef int(init_fnc_t) (void) 和 typedef int(*init_fnc_t) (void)
- typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)
- “typedef int (init_fnc_t) (void);“的含义
- typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void) 区别
- typedef int(init_fnc_t) (void) 和 typedef int(*init_fnc_t) (void);
- Linux内核之 “typedef int (init_fnc_t) (void);”
- typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)
- 自定义函数名数据类型typedef int (init_fnc_t) (void);
- typedef int(init_fnc_t) (void) 和 typedef int(*init_fnc_t) (void)
- 【转】typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)