【目录】

基本定义

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. Delphi 文件遍历

    unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  2. C#编程(五十六)----------可观察的集合ObservableCollection

    原文链接: http://blog.csdn.net/shanyongxu/article/details/47080685 可观察的集合 如果需要集合中的元素核实删除或添加的信息,就可以使用Obse ...

  3. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

  4. UITextField的简易封装

    UITextField的简易封装 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 UITextFieldView // ...

  5. Redis Nosql数据库

              Redis是一个key-value存储系统.和Memcached类似.可是攻克了断电后数据全然丢失的情况.并且她支持很多其它无化的value类型.除了和string外,还支持lis ...

  6. [DEFCON全球黑客大会] CTF(Capture The Flag)

    copy : https://baike.baidu.com/item/ctf/9548546?fr=aladdin CTF(Capture The Flag)中文一般译作夺旗赛,在网络安全领域中指的 ...

  7. SVG.js 基础图形绘制整理(二)

    一.折线 var draw = SVG('svg1').size(300, 300); //画折线 //使用字符串点 // var polyline=draw.polyline('0,0 100,50 ...

  8. 如何解决rar文件解压缩失败

    附件经常会是一系列的压缩文件,下载是默认文件名是一个随机数字.因而下载完会出现压缩文件解压缩失败解决方法:下载时重命名为带一定顺序的文件名,如文件1,文件2,文件3等 如何解决单个文件解压失败?论坛中 ...

  9. 创想三维:5款最好用的免费3D建模软件【转】

    虽然网上有需要现成的免费三维模型,但对于许多人而言,3D打印机最吸引他们之处是可以设计创造完全属于自己的模型.问题是,现代专业级CAD软件大多价格高昂,例如Solidworks或Zbrush这样的程序 ...

  10. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...