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

来源:互联网 发布:软件物理架构 编辑:程序博客网 时间:2024/06/07 10:07

//  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/

原创粉丝点击