回调函数例子(结构)--已测试

时间:2021-04-15 17:37:01

#include <stdlib.h>
#include <stdio.h>
int MyTest1()
{
   int i;
   for (i=0; i<30; i++)
   {
     printf("The %d th charactor is: %c/n", i, (char)('a' + i%26));
   }
   return 0;
}

int MyTest2(int num)
{
   int i;
   for (i=0; i<num; i++)
   {
    printf("The %d th charactor is: %c/n", i, (char)('a' + i%26));
   }
   return 0;
}

struct TestStruct
{
 int (*test1)();
 int (*test2)(int n);
 int (*test3)();
};
struct TestStruct MyTestStruct=
{
 MyTest1,
 MyTest2,
 NULL
};

struct TestStruct *get_test_ops(void)
{
 return &MyTestStruct;
}


int main()

{
   printf("************************/n");
   TestStruct* my=get_test_ops();;
   my->test1();
   my->test2(30);
   return 0;
}