欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 结构体中的函数指针

结构体中的函数指针

2025/2/25 1:29:43 来源:https://blog.csdn.net/weixin_42550185/article/details/144950989  浏览:    关键词:结构体中的函数指针

结构体中的函数指针,在linux中比较常见,所以很有必要学习。

1、在linux中的例子:

struct platform_driver {
    int (*probe)(struct platform_device *);
    int (*remove)(struct platform_device *);
    void (*shutdown)(struct platform_device *);
};


static int panel_simple_platform_probe(struct platform_device *pdev)
{
}


static int panel_simple_platform_remove(struct platform_device *pdev)
{
}


static void panel_simple_platform_shutdown(struct platform_device *pdev)
{

}

/*初始化platform_driver结构体*/
static struct platform_driver panel_simple_platform_driver = {
    .probe = panel_simple_platform_probe,
    .remove = panel_simple_platform_remove,
    .shutdown = panel_simple_platform_shutdown,
};

2、在C语言中的例子:

#include "stm32f10x.h"
#include "USART1.h"
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()
#include "stdlib.h"
#include <string.h> 

/*
结构体中的函数指针,在linux中比较常见,所以很有必要学习。
*/

//声明my_driver_struct结构类型
struct my_driver_struct {
    int (*FunctionPointer1)(char x);
    void (*FunctionPointer2)(void);
};
struct my_driver_struct my_driver;

int Function_1(char x)
{
    if(x) printf("Function_1();\r\n");
    return 0;
}

void Function_2(void)
{
    printf("Function_2();\r\n");
}

//函数功能:my_driver结构初始化
void my_driver_init(void)
{
    my_driver.FunctionPointer1=Function_1;//给函数指针FunctionPointer1赋值
    my_driver.FunctionPointer2=Function_2;//给函数指针FunctionPointer2赋值
}

int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
    USART1_Serial_Interface_Enable(115200);
    printf("\r\nCPU reset\r\n");

    my_driver_init();//my_driver结构初始化
    while(1)
    {
      my_driver.FunctionPointer1(1);
      my_driver.FunctionPointer2();
    }
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词