美文网首页
函数指针

函数指针

作者: 李永开 | 来源:发表于2021-08-02 15:33 被阅读0次

    一.函数指针,指针指向函数

    //
    //  main.c
    //  cdemo
    //
    //  Created by liyongkai on 2021/6/6.
    //
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    int func(int a, char b) {
        printf("hello world\n");
        return 1;
    }
    
    int main(int argc, const char * argv[]) {
       
        //1.使用typedef
        typedef int (FUNC_TYPE)(int, char);
        FUNC_TYPE *p = func;
        //以下三种方式都能正确打印
        func(1, 'c');
        p(1, 'c');
        (*p)(1, 'c');
        /**
         p(1, 'c');
         (*p)(1, 'c');
         直接调用p和使用 *p的结果是一样的,是因为直接调用p的话系统会帮我们调用*p.
         */
        
        //2.使用typedef + *
        typedef int (*P_FUNC_TYPE)(int, char);
        P_FUNC_TYPE pp = func;
        pp(1, 'c');
        
        //3.直接引用
        int (*pFunc)(int, char) = func;
        pFunc(1, 'c');
    
        return 0;
    }
    

    二.函数指针做参数

    相关文章

      网友评论

          本文标题:函数指针

          本文链接:https://www.haomeiwen.com/subject/guxvmltx.html