C 语言中struct的函数实现

#include <stdio.h>


typedef struct _test
{
    void (*pFunction)();
}STest;

void display()
{
    printf("hello function\n");
}


void main(void)
{
    STest test;
    test.pFunction = display;

    test.pFunction();
}

C语言中不像C++能够直接定义函数,以前学习数据结构用的是C++版的数据结构,对C++的新特性没有说明,导致以前一直不知道这个问题。

最近在看linux驱动资料,发现struct全都是使用的函数指针的方式编写,百度之后才知道linux下struct中直接写函数是不能够编译的、

posted @ 2016-07-13 14:55  吃猫粮的耗子  阅读(2140)  评论(0)    收藏  举报