u-boot中typedef应用解析___init_fnc_t *init_sequence[](

本文通过实例深入解析了C语言中typedef的作用及如何使用typedef定义函数指针数组,进而实现对一系列函数的遍历调用。通过对比两种不同的typedef声明方式,帮助读者更好地理解函数指针的应用。
typedef在C中真是一个神奇的东西,没有点事例真是很难理解:(
回头看看那超烂的大学C教程,很多地方没写清楚,遇到问题时看不懂代码。

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("test0/n");
    return 0;
}

int test1 (void)
{
    printf("test1/n");
    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("test0/n");
    return 0;
}

int test1 (void)
{
    printf("test1/n");
    return 0;
}

 

 

 

 

<think>嗯,用户给了一个关于C语言函数指针的问题,特别是关于typedef定义函数指针类型init_fnc_t。我需要详细解释这个typedef的作用,以及如何在实际中使用它。首先,我应该解释什么是函数指针,然后说明typedef如何简化函数指针的声明。 用户可能希望了解如何声明和使用这种类型的函数指针,以及具体的代码示例。比如,如何用这个类型来声明变量,如何赋值,如何调用函数。然后,我需要给出一个完整的代码例子,展示定义多个初始化函数,将它们放入一个数组中,然后遍历执行这些函数。 在代码示例中,可能需要定义几个返回int的函数,比如board_init、device_init等。然后创建一个init_fnc_t类型的数组,把这些函数的地址放进去。最后循环调用每个函数,检查返回值是否为0,如果有错误就处理。 解释部分需要说明每个步骤的作用,比如typedef如何让代码更易读,数组的使用如何方便管理多个初始化函数。还要提到错误处理的部分,比如当某个初始化失败时打印错误信息并退出。 然后,根据用户的要求,提出相关问题。这些问题应该围绕函数指针和typedef展开,比如函数指针的其他用途,数组中的函数指针,typedef的作用,以及动态调用函数的方法。确保问题涵盖不同的应用场景和深入的概念,帮助用户进一步学习。 检查有没有遗漏的点,比如函数指针的语法是否正确,数组初始化的方式是否准确,以及错误处理是否合理。确保代码能够编译运行,没有语法错误。同时,解释部分要清晰,让读者能够理解每个部分的作用和目的。</think>这是一个C语言函数指针的typedef定义,用于简化函数指针类型的声明。以下详细解释和用法示例: ```c #include <stdio.h> // 定义函数指针类型(无参数,返回int) typedef int (*init_fnc_t)(void); // 示例初始化函数 int board_init(void) { printf("Board initialized\n"); return 0; } int device_init(void) { printf("Device initialized\n"); return 0; } int config_init(void) { printf("Config initialized\n"); return -1; // 模拟初始化失败 } int main() { // 使用typedef声明函数指针数组 init_fnc_t init_sequence[] = { board_init, device_init, config_init, NULL // 哨兵值 }; // 遍历执行初始化序列 for (int i = 0; init_sequence[i]; i++) { int ret = init_sequence[i](); if (ret != 0) { printf("Initialization failed at step %d\n", i+1); return ret; } } printf("All initializations succeeded\n"); return 0; } ``` 代码解释: 1. `typedef int (*init_fnc_t)(void)` 定义了一个名为init_fnc_t的函数指针类型 2. 该类型指向的函数特征:无参数,返回int类型 3. main函数中声明了init_fnc_t类型的数组,用于存储初始化函数指针 4. 遍历数组执行初始化函数,并检查返回值 典型应用场景: - 系统初始化序列管理 - 插件式架构开发 - 回调函数管理 - 命令模式实现
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值