- 论坛徽章:
- 0
|
本帖最后由 zhanghuafeng25 于 2013-06-21 09:25 编辑
例程如下 :
#include <stdio.h>
struct A
{
// this is data
int data;
// this is operation
void (*function)();
};
int myfun2(int a)
{
printf("this is fun(%d)\n", a);
return a;
}
char myfun3(int a)
{
printf("this is fun(%c)\n", a);
return a;
}
int main(void)
{
struct A a;
int b=10;
a.function = (void *)myfun2;
a.function('a');
a.function = (void *)myfun3;
a.function('a');
return 0;
}
这个程序在gcc下编译通过也能输出结果,可是我把struct A a,换成指针类型的struct A *a,用gcc编译能通过,但是不能输出结果,不知道什么原因,请高手指教。修改后的程序如下:
#include <stdio.h>
struct A
{
// this is data
int data;
// this is operation
void (*function)();
};
int myfun2(int a)
{
printf("this is fun(%d)\n", a);
return a;
}
char myfun3(int a)
{
printf("this is fun(%c)\n", a);
return a;
}
int main(void)
{
struct A *a;
int b=10;
a->function = (void *)myfun2;
a->function(b);
a->function = (void *)myfun3;
a->function('a');
return 0;
} |
|