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

    本文参考http://www.prglab.com/cms/pages/c-tutorial/advanced-data/pointers.php

    http://blog.csdn.net/ameyume/article/details/8220832

    1.函数指针

    函数指针是一个指针,其指向的是一个函数,而不是普通的数据类型或者类对象。其定义如下:

    int (*func)(int a, int b);     // 声明函数指针

    函数指针的最大作用是将一个函数作为参数传递给另一个函数。函数指针的声明与声明一个普通的函数原型差不多,除了函数指针的函数名需要被括在括号内,并在函数名前添加星号asterisk(*)。这就说明函数指针函数名是一个指针,上面的函数指针func定义为一个指向一个返回值为整型,有两个参数并且两个参数都是整型的函数

    下面是求两个数的和和差的函数指针用法:

    // pointer to functions
    #include <iostream.h>
    using namespace std;
    //求两个数的和
    int addition (int a, int b) { 
        return (a+b); 
    }
    
    //求两个数的差
    int subtraction (int a, int b) { 
        return (a-b);
     }
    int (*minus)(int,int) = subtraction; 
    int operation (int x, int y, int (*functocall)(int,int)) { 
        int g; 
        g = (*functocall)(x,y); 
        return (g); 
    } 
    
    int (*func)(int,int);                //声明函数指针
    int main () {
        int m,n;
        /*以函数指针的形式来调用
        func=addition;
        int res=(*func)(7,5);
        func=subtraction;
        int res1=(*func)(20,res); */
        m = operation (7, 5, addition);             //调用求和函数
        n = operation (20, m, minus);             //调用求差函数
        cout <<n; 
        system("pause");
        return 0; 
    }     

    2.指针函数

    指针函数是带指针的函数,其本质是一个函数,只是返回类型是某一类型的指针

    其定义如下:

    类型说明符  (*)函数名 (参数列表);
      int       *  func  (int x,int y);

    由上面的定义可以看出,指针函数是一个返回值为地址类型的函数。需要注意的是:函数的返回值必须由同类型的指针变量来接受(也就是说,指针函数一定具有返回值)。其返回值的用法如下:

    char *fun();        //声明一个返回类型为char类型的指针函数
    char *p;             //定义一个char类型的指针变量,用来接收fun()的返回值
    p=fun();            

    指针函数的使用实例:

    #include<iostream>
    using namespace std;
    
    int * GetDate(int week,int day);            //声明指针函数
    
    int main()
    {
        int week,day;
        do{
        cout<<"please enter week(1--5)   day(1--7)"<<endl;
        cin>>week>>day;
        }while(week<1||week>5||day<1||day>7)
        cout<<*GetDate(week,day);                //去引用获得该地址下的元素值
        cout<<endl;
        system("pause");
        return 0;
    }
        
    int * GetDate(int week,int day)
    {
        static int calendar[5][7]=
        {
            {1,2,3,4,5,6,7},
            {8,9,10,11,12,13,14},
            {15,16,17,18,19,20,21},
            {22,23,24,25,26,27,28},
            {29,30,31,-1}
        };
        return &calendar[week-1][day-1];            //返回指向calendar某个元素的地址
    }    

    以下例子参考http://blog.csdn.net/ameyume/article/details/8220832,以说明指针函数的使用过程中内存地址的转化

    #include<iostream>
    using namespace std;
    int *f(int a, int b); // 声明指针函数  
    int main()
    {
        cout << "------------------------------Start
    ";
        int *p1 = NULL;
        cout << "The memeory address of p1 = 0x" << p1 << endl;
        p1 = f(1, 2);
        cout << "The memeory address of p1 = 0x" << p1 << endl;
        cout << "*p1 = " << *p1 << endl;
        cout << "------------------------------End
    ";
        system("pause");
        return 0;
    }
    /*
    * 指针函数的定义
    * 返回值是指针类型int *
    */
    int *f(int a, int b) {
        int *p = (int *)malloc(sizeof(int));
        cout << "The memeory address of p = 0x"<<p << endl;
        memset(p, 0, sizeof(int));
        *p = a + b;
        cout << "*p = " << *p << endl;
        return p;
    }

    运行结果如下:

  • 相关阅读:
    【转】微信小程序原理
    【转】onAttachedToWindow()在整个Activity生命周期的位置及使用
    中序遍历非递归遍历算法
    多项式加法运算
    中缀表达式转换为后缀表达式
    多项式计算
    最大子列和问题
    广度优先搜索
    广搜和深搜的区别
    cookie 与 session 的区别详解
  • 原文地址:https://www.cnblogs.com/runningRain/p/5930223.html
Copyright © 2011-2022 走看看