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

在一些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如果你理解了,你可以继续翻阅你的源码了!

相关推荐
Ada's2 小时前
【计算机基础系列】003:Python数据结构
开发语言·数据结构·python
2601_962885723 小时前
如何用 Python 计算 TRIX 三重指数平滑均线指标?
开发语言·python
还卿一钵无情泪3 小时前
Unsloth 微调 构建自己的大模型 没有GPU也能微调
linux·开发语言·人工智能·python·大模型·nlp·unsloth
Nil2084 小时前
leetcode 118杨辉三角
算法·leetcode·职场和发展
倔强的石头1065 小时前
应用账号最小权限实践:读写账号、报表账号、运维账号分层
java·运维·开发语言
传人once5 小时前
页面中心圆圈放大效果如何写
开发语言·javascript·ecmascript
NeilYuen5 小时前
【C++】STL源码仿写(二):vector
开发语言·c++
蜗牛互联网6 小时前
Python消费Responses SSE事件:增量文本、超时与取消
java·开发语言·人工智能·后端·python
郝学胜_神的一滴6 小时前
游戏引擎原理与实践 04:拆解引擎基础系统与内存管理
c++·游戏
wanderist.6 小时前
线性筛法详解:从筛质数到欧拉函数、Möbius 函数与约数函数
java·数据结构·算法