分享

函数指针的理解,用代码就可以看懂

 GLL_ 2018-08-01

​#include <stdio.h>

int add(int x, int y)
{
    printf("enter add\n");

    return x + y;
}
/*
 *定义一个指针变量
 *该变量中存储地址
 *存储函数的地址
 *存储返回值是int ,参数是int,int类型函数的地址
 * */

int (*pfunc)(int, int);  //定义函数指针

/*
 *PFUNC 是一种数据类型的别名
 *     返回值为int ,参数为int,int类型函数指针别名
 * */

typedef int (*PFUNC)(int, int);
int main(void)
{
    int res = 0;
    pfunc = add;
    printf("1+2=%d\n", pfunc(1,2));
    /*定义指针变量*/
    PFUNC test;
    test = add;
    printf("10+20=%d\n", test(10,20));

    PFUNC test1 = (PFUNC)0x0000000000400544;
    printf("100+200=%d\n", test1(100,200));

    res = (*((int (*)(int,int))0x0000000000400544))(1000,2000);
    printf("1k+2k=%d\n", res);

    return 0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    来自: GLL_ > 《c》

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多