您的位置:首页 > 其它

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

2011-06-17 17:05 225 查看
// 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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐