您的位置:首页 > 编程语言 > C语言/C++

C语言技巧--在结构体中使用函数例子(定义一个指向函数的指针)

2010-12-15 14:18 686 查看
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#define DECLARATION int (*ptr)(int x,int y)
#define DEFINITION(x,y) ptr(x,y)
int sum(int x,int y){
return(x+y);
}
void main()
{
int a,b,c;
typedef struct {
int r;
DECLARATION;
//int (*ptr)(int x,int y);
} mystr;
scanf("%d,%d",&a,&b);
//////////////////////////////////
//mystr stru;
//stru.ptr=sum;
//c=stru.ptr(a,b);
//printf("a=%d,b=%d,sum=%d/n",a,b,c);
///////////////////////////////////

typedef mystr *struc;
struc stru;
stru=(struc)malloc(sizeof(mystr));
memset(stru,0,sizeof(mystr));
stru->ptr=sum;
c=stru->DEFINITION(a,b);
//c=stru->ptr(a,b);
printf("a=%d,b=%d,sum=%d/n",a,b,c);
memset(stru,0,sizeof(mystr));
free(stru);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐