函数指针的几种不同用法

函数指针的用法

函数指针的几种不同用法(基础)

LCC-Win32 3.3 (Oct 6,2004)编译

=============================

#include <stdio.h>


void func(int i)
{
  printf("This is for test %i\r\n", i);
}

typedef void (*PFUNC)(int);

struct FUNC{
  PFUNC pfunc;
};

void callfunc(void pfunctions(int), int i)
{
  pfunctions(i);
}

int main(int argc, char* argv[])
{
  void (*pfunc)(int);         //定义一个 形参int,返回值类型void的 函数指针
  pfunc = &func;              //为该函数指针赋值
  pfunc(1);

  //函数指针作为函数参数

  callfunc(pfunc, 2);

  //函数指针作为结构体成员

  struct FUNC sfunc;
  sfunc.pfunc = &func;
  sfunc.pfunc(3);

  return 0;
}

 

======================

Greg Shaw(本站特约作者)

作者:greg   更新日期:2004-11-07
来源:本站   浏览次数:

相关文章

相关评论   发表评论