#include <stdio.h>
#include <stdlib.h>
int fun1()
{
printf("this is fun1 call\n");
return 1;
}
void fun2(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
}
int main()
{
int (*pfun1)() = NULL;
void (*pfun2)(int, char) = NULL;
int a,b;
pfun1 = fun1; //第一种赋值方法
a = pfun1(); //第一种调用方法(推荐)
printf("%d\n",a);
b = (*pfun1)();//第二种调用方法
printf("%d\n",b);
pfun2 = &fun2;//第二种赋值方法(推荐,因为和其他数据指针赋值方法一致)
pfun2(1,'a');
(*pfun2)(2,'b');
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void fun(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
}
void fun1(void (*pfun)(int, char), int a, char c)
{
pfun(a, c);
}
int main()
{
fun1(fun, 1, 'a');
return 0;
}
// c++ 的形式差不多
// c 形式
#include <stdio.h>
#include <stdlib.h>
void fun(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
}
//fun1 函数的参数为double,返回值为函数指针void(*)(int, char)
void (*fun1(double d))(int, char)
{
printf("%f\n",d);
return fun;
}
int main()
{
void (*p)(int, char) = fun1(3.33);
p(1, 'a');
return 0;
}
//c++ 形式
#include <iostream>
using namespace std;
class test
{
public:
int fun(int a, char c)
{
cout<<"this is fun call:"<<a<<" "<<c<<endl;
return a;
}
};
class test2
{
public:
// test2 的成员函数fun1,参数是double,
//返回值是test的成员函数指针int(test::*)(int, char)
int (test::*fun1(double d))(int, char)
{
cout<<d<<endl;
return &test::fun;
}
};
int main()
{
test mytest;
test2 mytest2;
int (test::*p)(int, char) = mytest2.fun1(3.33);
(mytest.*p)(1, 'a');
return 0;
}
#include <stdio.h>
#include <stdlib.h>
float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;}
int main()
{
//定义一个函数指针数组,大小为2
//里面存放float (*)(float, float)类型的指针
float (*pfunArry[2])(float, float) = {&add, &minu};
double k = pfunArry[0](3.33,2.22);// 调用
printf("%f\n", k);
k = pfunArry[1](3.33,2.22);
printf("%f\n", k);
return 0;
}
//c++ 可类比
#include <stdio.h>
#include <stdlib.h>
float add(float a,float b)
{
printf("%f\n",a+b);
return a+b;
}
float minu(float a,float b)
{
printf("%f\n",a-b);
return a-b;
}
//用pfunType 来表示float(*)(float, float)
typedef float(*pfunType)(float, float);
int main()
{
pfunType p = &add;//定义函数指针变量
p(3.33, 2.22);
pfunType parry[2] = {&add, &minu};//定义函数指针数组
parry[1](3.33, 2.22);
//函数指针作为参数可以定义为:void fun(pfunType p)
//函数指针作为返回值可以定义为:pfunType fun();
return 0;
}
//c++ 可类比
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有