通过结构体、地址调用函数,函数的一些另类调用方式

14 篇文章 0 订阅

通过结构体调用程序

下面代码来至于rt-thread操作系统的设备驱动。(C也能面向对象,类似java里的属性+方法)

//定义结构体
struct rt_wlan_cfg_ops
{
    int (*read_cfg)(void *buff, int len);
    int (*get_len)(void);
    int (*write_cfg)(void *buff, int len);
};
//结构体赋值
static const struct rt_wlan_cfg_ops ops =
{
    read_cfg,
    get_len,
    write_cfg
};
//把链接付给另外一个结构体
static const struct rt_wlan_cfg_ops *cfg_ops;
rt_wlan_cfg_set_ops(&ops);

void rt_wlan_cfg_set_ops(const struct rt_wlan_cfg_ops *ops)
{
    rt_wlan_cfg_init();

    WLAN_CFG_LOCK();
    /* save ops pointer */
    cfg_ops = ops;
    WLAN_CFG_UNLOCK();
}

//通过结构体调用函数
cfg_ops->write_cfg(info_pkg, len)

使用函数指针来调用地址为app_addr的函数

通过函数地址调用函数,应用于qboot。
运行boot程序,跳转到固定的app程序地址。

typedef void (*app_func_t)(void);
app_func_t app_func = (app_func_t)(*((__IO uint32_t *)(app_addr + 4)));
app_func();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值