函数名与函数指针

1.通常的函数调用和通过函数指针变量调用函数

先看两段代码

//代码7.1
#include <iostream>

using namespace std;

void MyFun(int x);

int main()
{
    MyFun(10);
}

void MyFun(int x)
{
    cout << x << endl;
}

//代码7.2
#include <iostream>

using namespace std;

void MyFun(int x);
void (*FunP)(int );

int main()
{
    MyFun(10);
    FunP = &MyFun;
    (*FunP) (20);
}

void MyFun(int x)
{
    cout << x << endl;
}

代码7.1的运行截图:

代码7.2的运行截图:

比较两段代码,结合前几篇笔记,发现MyFun好像是一个int型变量,FunP好像是一个int型的指针变量,这是我的感觉

2.调用函数的其他写法

说实话,看到这几种写法确实填充了我的知识漏洞,第一次知道可以有那么多调用方式

//代码7.3
#include <iostream>

using namespace std;

void MyFun(int x);
void (*FunP)(int );

int main()
{
    MyFun(10);
    //FunP = &MyFun;
    FunP = MyFun; //新的写法
    FunP(20); //还有这一行
    //(*FunP)(20);
}

void MyFun(int x)
{
    cout << x << endl;
}


//代码7.4
#include <iostream>

using namespace std;

void MyFun(int x);
void (*FunP)(int );

int main()
{
    MyFun(10);
    FunP = &MyFun;//注意新的写法
    //FunP = MyFun;
    FunP(20);//还有这一行
    //(*FunP)(20);
}

void MyFun(int x)
{
    cout << x << endl;
}
    

//代码7.5
#include <iostream>

using namespace std;

void MyFun(int x);
void (*FunP)(int );

int main()
{
    MyFun(10);
    //FunP = &MyFun;
    FunP = MyFun;
    //FunP(20);
    (*FunP)(20);
}

void MyFun(int x)
{
    cout << x << endl;
}

以上所有代码均可以运行,且运行结果正确

另外:

//代码7.6
#include <iostream>

using namespace std;

void MyFun(int x);

int main()
{
    (*MyFun)(10);//注意这行
}

void MyFun(int x)
{
    cout << x << endl;
}

注意与7.1代码的比较,发现调用MyFun函数又多了一种

总结思考:

1)其实,MyFun 的函数名与 FunP 函数指针都是一样的,即都是函数指针。 MyFun 函数名是一个函数指针常量,而 FunP 是一个函数数指针变量,这是它 们的关系。

2)但函数名调用如果都得如(*MyFun)(10)这样,那书写与读起来都是不方便和不习惯的。所以 C 语言的设计者们才会设计成又可允许 MyFun(10)这种 形式地调用(这样方便多了并与数学中的函数形式一样,不是吗?)。

3)为统一起见,FunP 函数指针变量也可以 FunP(10)的形式来调用。

4)赋值时,即可 FunP = &MyFun 形式,也可 FunP = MyFun。

3. 定义某一函数的指针类型

//代码7.7
#include <iostream>

using namespace std;

void MyFun(int );
void typedef (*FunType)(int );

int main()
{
    MyFun(10);
    FunType FunP;
    FunP = &MyFun;//FunP = MyFun;也可以
    FunP(20);
    (*FunP)(30);
}

void MyFun(int x)
{
    cout << x << endl;
}

下面是程序运行截图

分析如下:

首先,在 void (*FunType)(int)前加了一个 typedef 。这样只是定 义一个名为 FunType 函数指针类型,而不是一个 FunType 变量。 然后,“FunType FunP;”声明一个 FunP 变量。 有了 FunType 类型后,我们就可以同样地、很方便地用 FunType 类型来 声明多个同类型的函数指针变量了。如下: FunType FunP2; FunType FunP3;

4. 函数指针作为某个函数的参数

我们先来看一段代码

//代码7.8
#include <iostream>

using namespace std;

void MyFun1(int x);
void MyFun2(int x);
void MyFun3(int x);
typedef void (*FunType)(int ); /* 2. 定义一个函数指针类型FunType,与1函数类型一致 */
void CallMyFun(FunType myfun,int x);

int main()
{
    CallMyFun(MyFun1,10); /* 5. 通过 CallMyFun 函数分别调用三个不同的函数 */
    CallMyFun(MyFun2,20);
    CallMyFun(MyFun3,30);
}

void MyFun1(int x)/* 1. 这是个有一个参数的函数,以下两个函数也相同。 */
{
    cout << x << endl;
}

void MyFun2(int x)
{
    cout << x << endl;
}

void MyFun3(int x)
{
    cout << x << endl;
}

void CallMyFun(FunType myfun,int x)/* 3. 参数 fp 的类型是FunType。*/
{
    //(*myfun)(x);/* 4. 通过 myfun 的指针执行传递进来的函数,注意 myfun 所指的函数是有一个参数的。 */ 
    myfun(x);//上面写法一样正确
}

可以按照注释1-5的顺序进行分析

代码7.8的运行结果如下所示

10
20
30

      /* C指针笔记全部结束*/

/* 2021.11.18 */

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值