【目录】

基本定义

c 函数指针使用举例

c++ 函数指针使用举例

函数指针作为函数参数

函数指针作为函数返回值

函数指针数组

typedef 简化函数指针操作


c语言函数指针的定义形式:返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…);

c++函数指针的定义形式:返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….);    

以下代码编译环境:codeblocks with gcc in win 7

c语言函数指针使用举例:

#include <stdio.h>
#include <stdlib.h> int fun1()
{
printf("this is fun1 call\n");
return ;
} 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(,'a');
(*pfun2)(,'b');
return ;
}

c++函数指针使用举例:

#include <iostream>
using namespace std; class test
{
public:
test()
{
cout<<"constructor"<<endl;
}
int fun1(int a, char c)
{
cout<<"this is fun1 call:"<<a<<" "<<c<<endl;
return a;
}
void fun2(double d)const
{
cout<<"this is fun2 call:"<<d<<endl;
}
static double fun3(char buf[])
{
cout<<"this is fun3 call:"<<buf<<endl;
return 3.14;
}
}; int main()
{
// 类的静态成员函数指针和c的指针的用法相同
double (*pstatic)(char buf[]) = NULL;//不需要加类名
pstatic = test::fun3; //可以不加取地址符号
pstatic("myclaa");
pstatic = &test::fun3;
(*pstatic)("xyz"); //普通成员函数
int (test::*pfun)(int, char) = NULL; //一定要加类名
pfun = &test::fun1; //一定要加取地址符号
test mytest;
(mytest.*pfun)(, 'a'); //调用是一定要加类的对象名和*符号 //const 函数(基本普通成员函数相同)
void (test::*pconst)(double)const = NULL; //一定要加const
pconst = &test::fun2;
test mytest2;
(mytest2.*pconst)(3.33); // //构造函数或者析构函数的指针,貌似不可以,不知道c++标准有没有规定不能有指向这两者的函数指针
// (test::*pcon)() = NULL;
// pcon = &test.test;
// test mytest3;
// (mytest3.*pcon)(); return ;
}

函数指针作为函数参数:

#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, , 'a');
return ;
}
// 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(, 'a');
return ;
}
//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)(, 'a');
return ;
}

函数指针数组:

#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[])(float, float) = {&add, &minu};
double k = pfunArry[](3.33,2.22);// 调用
printf("%f\n", k);
k = pfunArry[](3.33,2.22);
printf("%f\n", k);
return ;
}
//c++ 可类比

typedef 简化函数指针类型:

#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[] = {&add, &minu};//定义函数指针数组
parry[](3.33, 2.22);
//函数指针作为参数可以定义为:void fun(pfunType p)
//函数指针作为返回值可以定义为:pfunType fun(); return ;
}
//c++ 可类比

【版权声明】转载请注明出处  http://www.cnblogs.com/TenosDoIt/p/3164081.html

c/c++ 函数指针的用法的更多相关文章

  1. (转)typedef 函数指针的用法

    typedef 函数指针的用法   在网上搜索函数指针,看到一个例子.开始没看懂,想放弃,可是转念一想,这个用法迟早要弄懂的,现在多花点时间看懂它,好过以后碰到了要再花一倍时间来弄懂它.其实很多时候都 ...

  2. typedef 函数指针的用法

    转自:http://www.cnblogs.com/shenlian/archive/2011/05/21/2053149.html typedef 函数指针的用法 在网上搜索函数指针,看到一个例子. ...

  3. IOS typedef 函数指针的用法

    代码简化, 促进跨平台开发的目的. typedef 行为有点像 #define 宏,用其实际类型替代同义字. 不同点:typedef 在编译时被解释,因此让编译器来应付超越预处理器能力的文本替换. 用 ...

  4. C函数指针的用法

    1.最简单的用法: #include <cstdio> int (*p)(int);//定义一个函数指针变量p(下面的f其实是一个常量函数指针) int f(int x) { printf ...

  5. C++成员函数指针错误用法警示(成员函数指针与高性能的C++委托,三篇),附好多评论

    今天做一个成绩管理系统的并发引擎,用Qt做的,仿照QtConcurrent搞了个模板基类.这里为了隐藏细节,隔离变化,把并发的东西全部包含在模板基类中.子类只需注册需要并发执行的入口函数即可在单独线程 ...

  6. C语言函数指针的用法

    函数指针是一种在C.C++.D语言.其他类 C 语言和Fortran 2003中的指针.函数指针可以像一般函数一样,用于调用函数.传递参数.在如 C 这样的语言中,通过提供一个简单的选取.执行函数的方 ...

  7. C++ 指向类成员函数指针的用法(转自维基百科)

    类成员函数指针 类成员函数指针(member function pointer),是C++语言的一类指针数据类型,用于存储一个指定类具有给定的形参列表与返回值类型的成员函数的访问信息. 目录 1 语法 ...

  8. C中的函数指针的用法

    include<stdio.h> typedef int (*Cal)(int a,int b);//定义一个函数指针,第一个int是指向函数的返回值的类型,后面括号里面的两个int是指指 ...

  9. x64系统的判断和x64下文件和注册表访问的重定向(举例了GetProcAddress后转成函数指针的用法)

    判断一个32位应用程序是否运行在x64系统下,可以使用下面的代码: BOOL IsX64System() { BOOL bIsWow64 = FALSE; typedef BOOL (WINAPI * ...

随机推荐

  1. 详解Java Spring各种依赖注入注解的区别

    注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Comp ...

  2. yolo源码解析(一)

    原文:https://www.cnblogs.com/zyly/p/9534063.html yolo源码来源于网址:https://github.com/hizhangp/yolo_tensorfl ...

  3. Qt中对QDomDocument和QDomnode的理解

    一.对QDomDocument和QDomnode的理解 QDom前缀的都是代表节点类型.所以有,QDomElement代表一个Element节点,而QDomText代表一个Text节点.QDomNod ...

  4. 在linux下 用p7zip 命令行下 解压 iso文件

    todo tomorrorw! 在linux下可以通过 mount IOSFILE.iso /media/myiso/ -o loop 这种方式挂载,然后直接从 目录中拷贝 iso的内容出来就可以. ...

  5. Guava之Iterables使用示例

    这是一个常量工具类.Iterables类包含了一系列的静态方法,来操作或返回Iterable对象. public final class Iterables { private Iterables() ...

  6. Andriod源码搜集

    1.一个左侧抽屉式导航NavigationDraw 教程:http://developer.android.com/training/implementing-navigation/nav-drawe ...

  7. 第三十二章 elk(3)- broker架构 + 引入logback

    实际中最好用的日志框架是logback,我们现在会直接使用logback通过tcp协议向logstash-shipper输入日志数据.在上一节的基础上修改!!! 一.代码 1.pom.xml < ...

  8. AppWidgetProvider 桌面插件 Widget 广播 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. AS 阿里巴巴Java开发规约 CheckStyle-IDEA

    Alibaba Java Coding Guidelines 简介 github地址:https://github.com/alibaba/p3c  官方文档    阿里巴巴Java开发手册(纪念版) ...

  10. 备份VMware虚拟磁盘文件 移植到其他虚拟机

    原文:http://jingyan.baidu.com/article/a681b0de17b3173b1843468f.html 方法/步骤     第一种方法:直接复制本地主机磁盘下的虚拟磁盘文件 ...