详解 指针函数,函数指针,函数指针函数

在一些STL源码或者linux源码经常能看到这些东西,确实挺让人头疼

指针函数

返回值为指针的函数

复制代码
monney* get_monney(people* p) //传people指针进去 return的是这个人的财产指针
{
   return p->monney_;
}

函数指针

函数指针是一个指向函数的指针,允许通过它调用函数。

复制代码
#include<iostream>
using namespace std;
void text()
{
  cout<<"hello"<<endl;
}
void (*func)();       //声明了一个返回值为void参数为空的函数指针 func     
int main()
{
   
   func=text;              //text初始化func
   //void (*func)()=text;  //可以直接声明并且初始化
   func();                 //打印hello
   return 0;
}

指针函数指针

一个指向指针函数的指针

实质就是一个函数指针,其实没有指针函数指针这种说法

cpp 复制代码
#include <iostream>
using namespace std;

int *get(int &a) {
	return &a;
}

int main() {
	int a = 10;
	int *(*func)(int &) = get;        //声明一个返回值为int*参数为int的函数指针
	cout << func(a) << endl;
	return 0;
}

函数指针函数

返回值为函数指针的函数

cpp 复制代码
#include <iostream>
using namespace std;

void text() {
	cout << "hello" << endl;
}

void (*get_func())() {     //定义了一个函数get_func() 返回一个返回值为void 参数为空的函数指针
	return text;
}
int main() {
	void (*func)() = get_func();
	func();
	return 0;
}

检验一下

static void (*set_malloc_handler(void (*f)()))()

这是一个返回 返回值为void参数为空的函数指针 的set_malloc_handler函数,同时这个函数需要一个函数指针f为参数

ok如果你理解了,你可以继续翻阅你的源码了!

相关推荐
virus59453 小时前
悟空CRM mybatis-3.5.3-mapper.dtd错误解决方案
java·开发语言·mybatis
一匹电信狗3 小时前
【LeetCode_547_990】并查集的应用——省份数量 + 等式方程的可满足性
c++·算法·leetcode·职场和发展·stl
初次见面我叫泰隆3 小时前
Qt——3、常用控件
开发语言·qt·客户端
鱼跃鹰飞4 小时前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
Queenie_Charlie4 小时前
小陶的疑惑2
数据结构·c++·树状数组
无小道4 小时前
Qt——QWidget
开发语言·qt
时艰.4 小时前
Java 并发编程之 CAS 与 Atomic 原子操作类
java·开发语言
梵刹古音5 小时前
【C语言】 函数基础与定义
c语言·开发语言·算法
筵陌5 小时前
算法:模拟
算法
梵刹古音5 小时前
【C语言】 结构化编程与选择结构
c语言·开发语言·嵌入式