C语言结构体内部的函数指针有什么意义

2025-03-01 09:13:01
推荐回答(1个)
回答1:

//在结构体中包含函数指针,
//这样,可以使用结构体,调用函数。
//这个有点像C++的面向对象的类 
//十分好用。 
#include "stdio.h"  
struct DEMO  
{  
    int x,y;  
    int (*func)(int,int); //函数指针  
};  
int add2(int x,int y)  
{  
    return x+y;  
}  
int main()  
{
int ret=0;
    struct DEMO demo;  
    demo.func=&add2; //结构体函数指针赋值  
    ret=demo.func(3,4);
    printf("func(3,4)=%d\n",ret);  
}