zoukankan      html  css  js  c++  java
  • 函数名和函数指针

     调用时:(*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
    ",x);
    16 }
    View Code


    或者如下编译:  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
    ",x);
    20 }
    View Code

    或者:

    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
    ",x);
    20 }
    View Code

    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
    ",x);
    20 }
    View Code

    结论:
    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 )。

  • 相关阅读:
    邱洁红(帮别人名字作诗)
    为了你,我已等了一千年
    为什么才华横溢的人总是怀才不遇
    这三种力量,让你的人生从此大不一样……
    赠中华儿女
    管理的7重境界
    写下你人生101个不可思议的目标
    忙碌啊,请别带走我的诗魂
    宋彦澍(帮别人名字作诗)
    慈善家洛克菲勒先生的思想精华 自信与坚持
  • 原文地址:https://www.cnblogs.com/chensup/p/5809951.html
Copyright © 2011-2022 走看看