要使用不同的参数的函数的函数指针

http://support.microsoft.com/viewkb/viewkb.aspx?contentid=70142


The sample code below illustrates how to use function pointers to call functions that take different parameters than those with which the pointer was declared. The function is cast to the type of the function pointer during the first assignment, then the function pointer is cast to the type of the function to be called. Using typedefs generally makes it easier than casting the pointer directly.

Sample Code

/* Compile options needed: none
*/ 

#include <stdio.h>

typedef int  (*myintchartype) (char *, char *);
typedef void (*myvoidtype) (void);
typedef int  (*myintvoidtype) (void);

int func1(char *, char *);
void func2(void);

void main(void)
{
   myintvoidtype ptr;

   ptr = (myintvoidtype) func1;
   ((myintchartype) ptr)("one", "two");

   ptr = (myintvoidtype) func2;
   ((myvoidtype) ptr)();
}

int func1(char *a, char *b)
{
   return printf("func1 took two parameters: %s and %s\n", a, b);
}

void func2(void)
{
   printf("func2 did not take any parameters\n");
}
				

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值