函数名以和函数指针

调用时:(*FunP)(20);//这是通过函数指针变量FunP来调用MyFun函数的。

 1 void MyFun(int x);//此处的申明也可以写成:void MyFun(int);
 2 void (*FunP)(int);//此处的申明也可以写成:void (*FunP)(int);但习惯上一般不这样。
 3
 4 int _tmain(int argc, _TCHAR* argv[])
 5 {
 6     MyFun(10);//这是直接调用MyFun函数
 7     FunP = MyFun;//将MyFun函数的地址赋给FunP变量
 8     (*FunP)(20);//这是通过函数指针变量FunP来调用MyFun函数的。
 9
10     system("pause");
11     return 0;
12 }
13 void MyFun(int x)//这里定义一个MyFun函数
14 {
15     printf("%d\n",x);
16 }

或者如下编译:  FunP(20);//这是通过函数指针变量来调用MyFun 函数的。

 1 #include "stdafx.h"
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4
 5 void MyFun(int x);//此处的申明也可以写成:void MyFun(int);
 6 void (*FunP)(int);//此处的申明也可以写成:void (*FunP)(int);但习惯上一般不这样。
 7
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     MyFun(10);//这是直接调用MyFun函数
11     FunP = MyFun;//将MyFun函数的地址赋给FunP变量
12     FunP(20);//这是通过函数指针变量FunP来调用MyFun函数的。
13
14     system("pause");
15     return 0;
16 }
17 void MyFun(int x)//这里定义一个MyFun函数
18 {
19     printf("%d\n",x);
20 }

或者:

FunP=&MyFun;//将MyFun 函数的地址赋给FunP 变量
FunP(20);//这是通过函数指针变量来调用MyFun 函数的。

 1 #include "stdafx.h"
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4
 5 void MyFun(int x);//此处的申明也可以写成:void MyFun(int);
 6 void (*FunP)(int);//此处的申明也可以写成:void (*FunP)(int);但习惯上一般不这样。
 7
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     MyFun(10);//这是直接调用MyFun函数
11     FunP = &MyFun;//将MyFun函数的地址赋给FunP变量
12     FunP(20);//这是通过函数指针变量FunP来调用MyFun函数的。
13
14     system("pause");
15     return 0;
16 }
17 void MyFun(int x)//这里定义一个MyFun函数
18 {
19     printf("%d\n",x);
20 }

FunP = &MyFun;//将MyFun函数的地址赋给FunP变量
 (*FunP)(20);//这是通过函数指针变量FunP来调用MyFun函数的。

 1 #include "stdafx.h"
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4
 5 void MyFun(int x);//此处的申明也可以写成:void MyFun(int);
 6 void (*FunP)(int);//此处的申明也可以写成:void (*FunP)(int);但习惯上一般不这样。
 7
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     MyFun(10);//这是直接调用MyFun函数
11     FunP = &MyFun;//将MyFun函数的地址赋给FunP变量
12     (*FunP)(20);//这是通过函数指针变量FunP来调用MyFun函数的。
13
14     system("pause");
15     return 0;
16 }
17 void MyFun(int x)//这里定义一个MyFun函数
18 {
19     printf("%d\n",x);
20 }

结论:
1. 其实,MyFun 的函数名与FunP 函数指针都是一样的,即都是函数指针。MyFun 函数名是一个
函数指针常量,而FunP 是一个函数数指针变量,这是它们的关系。
2. 但函数名调用如果都得如(*MyFun)(10);这样,那书写与读起来都是不方便和不习惯的。所以
C 语言的设计者们才会设计成又可允许MyFun(10);这种形式地调用(这样方便多了并与数学中的函数
形式一样,不是吗?)。
3. 为统一起见,FunP 函数指针变量也可以FunP(10)的形式来调用。
4. 赋值时,即可FunP=&MyFun 形式,也可FunP=MyFun。

补充说明一点:在函数的申明处:
void MyFun(int );//不能写成void (*MyFun)(int )。
void (*FunP)(int );//不能写成void FunP(int )。

时间: 2024-07-30 19:52:56

函数名以和函数指针的相关文章

Delphi 调用C/C++的Dll(stdcall关键字, 会导致函数名分裂. 此时函数名变成[email&#160;protected])

delphi调用C++写的Dll, 当然这个Dll要求是非MFC的Dll, 这样子才能被delphi调用. 根据C++定义函数的情况, Delphi有不同的相对应的处理方法.1. 声明中不加__stdcall,采用VC默认格式__cdecl,但在Delphi中要注明调用格式为cdecl.C++中例子: [cpp] view plain copy print? extern "C" int __declspec(dllexport) add(int x, int y); Delphi中例

c++入门之——const在函数名前面和函数后面的区别

1 class Test(){ 2 public: 3 Test(){} 4 const int foo(int a); 5 const int foo(int a) const; 6 }; 一.概念 当const在函数名前面的时候修饰的是函数返回值,在函数名后面表示是常成员函数,该函数不能修改对象内的任何成员,只能发生读操作,不能发生写操作. 二.原理: 我们都知道在调用成员函数的时候编译器会将对象自身的地址作为隐藏参数传递给函数,在const成员函数中,既不能改变this所指向的对象,也不能

php函数名前添加&amp; 函数的引用返回

function &test(){ static $b=0; $b= $b+1; return $b; } $a= test(); echo $a,"\n"; $a = 3; $a= test(); echo $a,"\n"; $a = &test(); echo $a,"\n"; $a= 10; $a= test(); echo $a,"\n"; 结果: test()函数引用返回就是   $a 引用$b  

delphi通过函数名动态调用函数的方法

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TMyCacl = class(TObject) public { Public declarations } function DynamicFun(funName: string; const a, b: integer): integer;

依据函数名字符串执行函数

这个问题虽然不是很严重,但却困扰本人许久.曾经多方询问朋友.查阅资料均无法达到效果. 今日偶然查到一些东西,经实测的却可行,也算了却了一桩心愿. 不再废话直接贴DEMO代码 1 unit Unit2; 2 3 interface 4 5 uses 6 Vcl.Dialogs, Vcl.Forms, System.Generics.Collections, System.Variants; 7 8 type 9 TMyIntf = class(TForm) 10 public 11 class f

&quot;函中函&quot; -------------------- func2(func) -------------- 函数名可以当做函数的参数

def func(): print("吃了么")def func2(fn): print("我是func2") fn() # 执?传递过来的fn # 即 fn替换成func, 即执行fn() print("我是func2")func2(func) 原文地址:https://www.cnblogs.com/jack20181017/p/9893035.html

函数是对象,函数名值指针

如题,函数与函数名的关系就和对象与对象指针关系是一样的,如何理解呢? function sum(a,b){ return a+b; } 上述代码定义了一个名为sum的函数,其函数名sum与函数主体的关系如下: 和对象与对象指针的关系一样,一个函数可以有多个名字. function sum(a,b){ return a+b; } alert(sum(10,10));//20 var anotherSum = sum; alert(anotherSum(10,10));//20 上述代码将函数指针s

彻底搞定C指针-函数名与函数指针【转】

转自:http://blog.csdn.net/a1232345/article/details/43524371 函数名与函数指针 一 通常的函数调用    一个通常的函数调用的例子://自行包含头文件void MyFun(int x);    //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]){   MyFun(10);     //这里是调用MyFun(10);函数 return 0;} void MyFun(in

函数名与函数指针

博文转自:http://www.cnblogs.com/CBDoctor/archive/2012/10/15/2725219.html 一 通常的函数调用    一个通常的函数调用的例子:   //自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]) { MyFun(10); //这里是调用MyFun(10);函数 return 0; } void MyFun(int