zoukankan      html  css  js  c++  java
  • uc os相关的C语言知识点1-函数指针

    开始读uc os的代码了,发现很多C语言的东西,之前没搞懂的,慢慢积累。

    就象某一数据变量的内存地址可以存储在相应的指针变量中一样,函数的首地址也以存储在某个函数指针变量里的。这样,我就可以通过这个函数指针变量来调用所指向的函数了。

      形式1:返回类型(*函数名)(参数表) ,例子如下:

    #include<stdio.h>
    void (*funp)(int);  //定义一个函数指针,注意函数的返回类型和参数类型和指针的一致,才可以用。
    void print(int n);  //函数申明
    
    int  main(void)
    {
      funp=print; //指针赋值
      (*funp)(10); //调用
    
    }
    
    void print(int n)
    {
        printf("%d
    ",n);
    }

    形式2:typedef  返回类型(*新类型)(参数表)

    不要以为typedef的只能这样用:typedef A B;

    #include<stdio.h>
    typedef void (*funp)(int); //这里不是定义一个具体的函数指针,是定义了一个新类型
    void print(int n);  //函数申明
    int  main(void)
    {
      funp p;
      p =print; //指针赋值
      (*p)(10); //调用
    
    }
    
    void print(int n)
    {
        printf("%d
    ",n);
    }

    形式3:把这个函数指针,作为参数,传入另一个函数

    #include<stdio.h>
    void pp1(int);
    void pp2(int);
    void pp3(int);
    typedef void (*funp)(int); //定义一个函数指针类型
    void callpp(funp p,int n); //这个函数的一个参数是函数指针
    
    void pp1(int n)
    {
        printf("pp1 print out %d
    ",n);
    }
    
    void pp2(int n)
    {
        printf("pp2 print out %d
    ",2*n);
    }
    
    void pp3(int n)
    {
        printf("pp3 print out %d
    ",3*n);
    }
    
    void callpp(funp p,int n)
    {
        p(n);  //这里就是函数的运行了
    }
    int main()
    {
        callpp(pp2,5);
        callpp(pp1,5);
        callpp(pp3,5);
    }
  • 相关阅读:
    hadoop2 作业执行过程之reduce过程
    hadoop2 作业执行过程之map过程
    hadoop2 作业执行过程之yarn调度执行
    scala的下划线
    tomcat 配置系列1
    Windows Server 2008 __ Windows Server 2008R2
    dell技术中心
    戴尔服务器启动和raid设置(以dell r420为例)
    WinPE安装windows(dell r420)
    Red Hat enterprise linux 5.4 x64 + DELL R420 安装网卡驱动 (broadcom 5270)
  • 原文地址:https://www.cnblogs.com/nasduc/p/4741839.html
Copyright © 2011-2022 走看看