void pointer as unknown argument type 和C回调函数问题

//  FILE NAME:c_callback_funtions_void.c


#include <stdio.h>
#include <stdlib.h>   // exit()
#include<string.h>    // bzero()
#include<sys/time.h>
#include<sys/types.h>
#include<unistd.h>

typedef void TaskFunc(void* argument);

void do_something(TaskFunc* proc, void* argument)
{
    (*proc)(argument); // 函数这里调用。
}

void funtion_1(void* argument)
{
    char* p = (char*)argument;
    printf("funtion_1 print is : %s/n",p);
}

typedef struct _FOR_FUNTION2
{
    int a;
    char str[20];
}FOR_FUNTION2;
void funtion_2(void* argument)
{
    FOR_FUNTION2 *p = (FOR_FUNTION2*)argument;
    printf("funtion_2 print is : %d,%s/n",p->a,p->str);
}

int main(int argc,char* argv[])
{
    char str1[] = "this is funtion_1";
    /*  第一种方法 */
    void* argument_fun_1 = (void*)str1;
    do_something(&funtion_1,argument_fun_1);
    /*  第二种方法 */
    do_something(&funtion_1,(void*)str1);

    FOR_FUNTION2* p = (FOR_FUNTION2*)malloc(sizeof(FOR_FUNTION2));
    p->a = 100;
    memcpy(p->str,"this is funtion_2/0",sizeof("this is funtion_2"));
    /*  第一种方法 */
    void* argument_fun_2 = (void*)p;
    do_something(&funtion_2,argument_fun_2);
    /*  第二种方法 */
    do_something(&funtion_2,(void*)p);

printf("%d/n",sizeof("test")); // 注意这里是等于5

    return 0;
}

 

http://www.newty.de/fpt/intro.html

http://www.cplusplus.com/forum/general/158/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值