函数指针的基础
函数的指针
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#if 0
//定义一个函数类型
typedef int Func(int);
//函数名称就代表函数的入口地址 函数名称本身就是一个指针
int test(int a)
{
return a*a;
}
void main()
{
//用函数类型 定义一个函数指针
Func *myfun = NULL;
myfun = test;
printf("myfun1 = %d",myfun(2));
myfun = &test;
printf("myfun2 = %d",myfun(2));
//(*(*(test)));
system("pause");
}
#endif
#if 0
void f()
{
printf("hello world\n");
}
void main()
{
//直接定义了一个函数指针,并且赋值
void(*myf1)() = f;
myf1();
void(*myf2)() = &f;
myf2();
}
#endif
//////////////////////////////////////////////////////////////////
//函数指针做参数
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int add(int a,int b);
int libfun(int (*pDis)(int a,int b));
int main(void)
{
//直接定义了一个函数指针
int (*pfun)(int a,int b);
//函数名赋给函数指针,函数的入口地址赋给了pfunc
pfun = add;
libfun(pfun); //函数指针 做函数参数
}
int add(int a, int b)
{
return a + b;
}
int libfun(int (*pDis)(int a,int b))
{
int a , b;
a = 1;
b = 2;
printf("%d",pDis(a,b));
return 0;
}