栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言回调函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言回调函数

#include 
#include 
#include 
#include 

#include 
#include 


#ifdef WIN32
#  include 
#  define READ_3RD_ARG unsigned int
#else
#  include 
#  define READ_3RD_ARG size_t
#endif

#if LIBCURL_VERSION_NUM < 0x070c03
#error "upgrade your libcurl to no less than 7.12.3"
#endif

void* cb(void *arg)
{
    printf("call back function, pid=%ldn", pthread_self());
    return NULL;
}

void fun1(void)
{
    printf("fun1 calln");

    return;
}

void fun2(int a)
{
    printf("fun2 call a=%dn", a);

    return;
}

void fun3(int a, double b)
{
    printf("fun3 call a=%d,b=%lfn", a, b);
    return;
}

int fun4(void)
{
    printf("fun4 call backn");
    return 666;
}

int* fun5(int a)
{
    int* ptr = NULL;
    ptr = &a;
    printf("fun5 call bacl a=%dn", a);
    return ptr;
}

int main(void)
{
    pthread_t therad1;
    pthread_create(&therad1, NULL, cb, NULL);
    pthread_join(therad1, NULL);

    pthread_t thread2;
    pthread_create(&thread2, NULL, cb, NULL);
    pthread_join(thread2, NULL);

    void (*ptr)(void);              // ptr是无返回值无参数的函数指针
    ptr = fun1;
    ptr();
    (*ptr)();

    void (*ptr2)(int);              // ptr2是无返回直带一个参数的函数指针
    ptr2 = fun2;
    ptr2(666);

    void (*ptr3)(int, double);
    ptr3 = fun3;
    ptr3(12, 234.321);

    int (*ptr4)(void);
    ptr4 = fun4;
    int rtn = ptr4();
    printf("rtn=%dn", rtn);

    int* (*ptr5)(int);
    ptr5 = fun5;
    rtn = *ptr5(888);               // ptr5表示int*类型的函数指针,赋值给一个int类型变量需要解引用才可以使用
    printf("rtn=%dn", rtn);

    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1027188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号