首页 > 其他分享 >12 | C语言中的函数类型和函数指针类型

12 | C语言中的函数类型和函数指针类型

时间:2023-04-26 12:01:02浏览次数:33  
标签:12 函数 int age C语言 Func 类型 函数指针

函数类型和函数指针类型的区别,让我们先看一个例子

#include <iostream>
using namespace std;

typedef int(Func)(int);
typedef int(*Func_p)(int) ;

int f(int a){return a;}
int main(){
    Func_p p=f;
    Func* p_ptr=f;
    cout<<p(0)<<endl;
    cout<<p_ptr(1)<<endl;
}

这段代码的运行结果

0

1

相信大家看到这里应该明白怎么回事了。
函数类型*函数指针类型


这也就可以解释 map 的模板参数需要传进去一个 函数指针类型,而不是函数类型

bool MyCompare(const Person &p1, const Person &p2) {//普通的函数
    return (p1.age < p2.age) || (p1.age == p2.age && p1.name.length() < p2.name.length());
}
map<Person,int,decltype(&MyCompare)> group(MyCompare);

在补充一点就是 指针函数 是什么,指针函数返回指针的函数。下面举几个例子

//1
int* func(int,int);

//2
typedef int(*Func_p)(int) ;
Func_p func(int,int);

标签:12,函数,int,age,C语言,Func,类型,函数指针
From: https://www.cnblogs.com/mmxingye/p/17355218.html

相关文章

  • pid算法函数实现,c语言版
     #include<stdio.h>floatpid(floatsetpoint,floatprocess_variable,floatkp,floatki,floatkd,floatdt,float*integral,float*last_error){//Calculateerrorfloaterror=setpoint-process_variable;//Calculateintegral......
  • C语言基础知识
    一维数组inta[2]={1,2},一维数组名a代表的是数组第一个元素的地址,不代表数组中所有元素。二维数组inta[3][4]总共是12个元素,可以当作3行4列来看待,这十二个元素的名字依次是:a[0][0],a[0][1],a[0][2],a[0][3]a[1][0],a[1][1],a[1][2],a[1][3]a[2][0],a[2][1],a[2][2],a[2][3]......
  • Yuzuki Lizard 全志V851S开发板 –移植 QT5.12.9教程
    移植QT5教程(此教程基于docker版V851S开发环境)dockerpullregistry.cn-hangzhou.aliyuncs.com/gloomyghost/yuzukilizard编译依赖apt-getinstallrepogitgcc-arm-linux-gnueabihfu-boot-toolsdevice-tree-compilermtools\partedlibudev-devlibusb-1.0-0-devpython......
  • Go语言入门12(协程 goroutine)
    协程进程和线程进程​ 当运行一个应用程序的时候,操作系统会为这个应用程序启动一个进程。可以将这个进程看作一个包含了应用程序在运行中需要用到和维护的各种资源的容器。这些资源包括但不限于内存地址空间、文件和设备的句柄以及线程线程​ 一个线程是一个执行空间,这个空间......
  • 12.存钱问题
     问题分析:  代码:#include<stdio.h>voidmain(){ inti; doublemoney=0.0; for(i=0;i<5;i++) money=(money+1000.0)/(1+0.0063*12); printf("应存入的钱数为:%0.2f\n",money);}......
  • C++第四章课后习题4-12
    定义一个datatype类,能处理包含字符型,整形,浮点型3种类型的数据,给出其构造函数。1#include<iostream>2usingnamespacestd;34classDataType{5private:6chara;7intn;8floatx;9enum{10character,11intege......
  • 打卡12
    2.9设汉王的失算 这道题非常的简单,直接从2的0次方加到2的63次方即可#include<bits/stdc++.h>usingnamespacestd;intmain(){ doubleans=0; for(inti=0;i<64;i++) { ans+=pow(2,i); } cout<<ans<<endl;} 2.10马克思手稿中的数学题 设x为男人,y为女人,z为小孩则满足x......
  • C语言函数(交换数值问题)
    实现交换a,b的数值:直接法:#include<stdio.h>intmain(){inta=10,b=20,temp=0;printf("a=%db=%d\n",a,b);temp=a;a=b;b=temp;printf("a=%db=%d\n",a,b);}输出为:2010函数法:#include<stdio.h>intmain(){inta=10;intb=20;//交换函数s......
  • c语言中的链接属性和存储类型
    链接属性external属性:不在代码块中的函数和变量在缺省情况下都属于external链接属性。具有external属性的变量或者函数在其他源文件中无论被包含多少次,都指向同一个实体。#a.cintx;-----------......
  • C语言 指针也是数组
    #include<stdio.h>main(){char*p="abcdef";printf("%c",*p);printf("\n%c",*(p+1));printf("\n%c",p[0]);printf("\n%c",p[1]);getchar(); }    ......