//---- test function inside struct
typedef struct try {
void (*init)();
void (*set)();
void (*print)();
int x,y;
} foo;
void init(foo *p)
{
p->x=0;
p->y=0;
}
void set(foo *p, int x, int y)
{
p->x = x;
p->y = y;
}
void print(foo *p)
{
printf("x=%d, y=%d\n\r", p->x, p->y);
}
foo *initialize(void)
{
foo *p = (foo*)malloc(sizeof(foo));
p->init = init;
p->set = set;
p->print = print;
return p;
}
//-------------------------------------
//----- test
foo *p = initialize();
p->init(p);
p->set(p, 10, 2);
p->print(p);
p->set(p, 15, 12);
p->print(p);
没有评论:
发表评论