zoukankan      html  css  js  c++  java
  • c++/c关于函数指针

    顺便提一句:指针也是一种变量类型 和 int double 这些类型是一个级别 不同的是它的值是地址

          

    #include "stdafx.h"
    #include<stdlib.h>
    void visit(int n){
    printf("printf argcs: %d ",n);
    }
    void test(void (*fun)(int n),int j){

    (*fun)(j);
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    //定义一个函数指针fun 该指针可以指向任意的void类型 参数为int类型的函数名
    void (*fun)(int n);
    //既然是指针当然其值只能是地址类型的 因为是函数指针所以理所当然要函数地址
    fun=&visit;
    //在执行语句里*的意义是取内容 所以就(*fun)<==>visit();
    (*fun)(20);
    //函数指针可以指向函数指针的地址(只要是个同类型的函数地址都行嘛) 这是没有问题的
    //说白了 test(fun,20)<==>test(&visit,20)
    test(fun,20);

    //到这里估计大家都明白函数指针是怎么回事了 但还是有问题 为什么定义函数指针
    //指针变量要用括号包起来 猜想下面的表达方式行不行
    //这样一看你就懂了这分明就是一个返回值为void 指针类型的函数哇
    #if 0
    void *fun(int n);
    #endif
    system("pause");
    return 0;

    }

  • 相关阅读:
    0x02 枚举、模拟、递推
    0x01 位运算
    bzoj3529: [Sdoi2014]数表
    bzoj5216: [Lydsy2017省队十连测]公路建设
    POJ1789Truck History
    最小生成树模板
    POJ1258Agri-Net
    POJ1860Currency Exchange(SPFA)
    POJ3083Children of the Candy Corn
    POJ2503Babelfish
  • 原文地址:https://www.cnblogs.com/enjoyall/p/5432536.html
Copyright © 2011-2022 走看看