C回调函数的简单实例

来源:互联网 发布:淘宝首页装修雪花代码 编辑:程序博客网 时间:2024/04/30 01:47
原文地址:http://blog.csdn.net/ymt/article/details/5265083#comments 

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

int Test2(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;
}

void Caller1(int (*ptr)())
//指向函数的指针作函数参数
{
   (*ptr)();
}

void Caller2(int n, int (*ptr)(int n))
//指向函数的指针作函数参数,这里第一个参数是为指向函数的指针服务的,
{
   //不能写成void Caller2(int (*ptr)(int n)),这样的定义语法错误。
   (*ptr)(n);
   return;
}
int main()

{

   printf("************************/n");

   Caller1(Test1); //相当于调用Test1();

   printf("************************/n");

   Caller2(30, Test2); //相当于调用Test2(30);

   return 0;

}

/*
int main()

{

   printf("************************/n");

   Caller1(&Test1); //相当于调用Test1();

   printf("&&&&&&************************/n");

   Caller2(30, &Test2); //相当于调用Test2(30);

   return 0;

}
*/

原创粉丝点击