何謂 callback function 呢?
用Google找了一下.....嗯.......
簡單的說就是回傳某個函數的指標, 呼叫者便可透過這個函數指標直接執行函數...
以下是我找到的範例
//---------------------------------------------------
//說明:
// 這個是一個callback function...
// 由Google上找到的例子加以修改而成的...
//---------------------------------------------------
//若要測試,記得只能存成.c檔...
//(有warning,可以不用理)
//不可以存成.cpp檔...因為complier不會過...
//---------------------------------------------------
#include <stdio.h>
//就是structure
struct table
{
char id;
void *func;
};
void *funcA() { printf("This is funcA()\n"); }
void *funcB() { printf("This is funcB()\n"); }
void *funcC() { printf("This is funcC()\n"); }
void *funcD() { printf("This is funcD()\n"); }
const struct table tb[] = { {1, funcA},
{2, funcB},
{3, funcC},
{4, funcD} };
//宣告callback function的prototype(原型)
void (*func)(void);
int main(int argc,char *argv[])
{
int index, i;
index = 1;
for (i = 0; i < 4; i++)
{
if (index == tb[i].id)
{
func = tb[i].func;
func();//呼叫callback function
}
index++;
}
printf("如果在這裡呼叫callback function呢?\n");
func();
return 0;
}
輸出的結果:
This is funcA()
This is funcB()
This is funcC()
This is funcD()
如果在這裡呼叫callback function呢?
This is funcD()
Press any key to continue
心得:要用心體會呀!!!!
用Google找了一下.....嗯.......
簡單的說就是回傳某個函數的指標, 呼叫者便可透過這個函數指標直接執行函數...
以下是我找到的範例
//---------------------------------------------------
//說明:
// 這個是一個callback function...
// 由Google上找到的例子加以修改而成的...
//---------------------------------------------------
//若要測試,記得只能存成.c檔...
//(有warning,可以不用理)
//不可以存成.cpp檔...因為complier不會過...
//---------------------------------------------------
#include <stdio.h>
//就是structure
struct table
{
char id;
void *func;
};
void *funcA() { printf("This is funcA()\n"); }
void *funcB() { printf("This is funcB()\n"); }
void *funcC() { printf("This is funcC()\n"); }
void *funcD() { printf("This is funcD()\n"); }
const struct table tb[] = { {1, funcA},
{2, funcB},
{3, funcC},
{4, funcD} };
//宣告callback function的prototype(原型)
void (*func)(void);
int main(int argc,char *argv[])
{
int index, i;
index = 1;
for (i = 0; i < 4; i++)
{
if (index == tb[i].id)
{
func = tb[i].func;
func();//呼叫callback function
}
index++;
}
printf("如果在這裡呼叫callback function呢?\n");
func();
return 0;
}
輸出的結果:
This is funcA()
This is funcB()
This is funcC()
This is funcD()
如果在這裡呼叫callback function呢?
This is funcD()
Press any key to continue
心得:要用心體會呀!!!!
文章標籤
全站熱搜

static int callback(void *data, int argc, char **argv, char **azColName){ int i; fprintf(stderr, "%s: ", (const char*)data); for(i=0; i2015-05-10 14:50
#include
#include
#include
#include
#include "sqlite3.h"
//global var
char copy[1024] = "\0";
char ID[1024]="_0353610";
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; ifalse
{
strcat(copy,ID);//將b連在a的尾巴
strcpy(buffer,copy);
sendto(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&server,len);
} else
sendto(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&server,len);
}
closesocket(socket1);
system("pause");
return 0;
}
*****
#include
#include
#include
#include //// 引用 pthread 函式庫
#include
pthread_t threads[2];
pthread_mutex_t lock;
void *thread_socket1(void *v)
{
SOCKET socket1;
WSADATA wsaData;
int ErrorCode;
if(WSAStartup(MAKEWORD(2,1),&wsaData))// windows socket dll;
{
printf("Winsock initiate failed!!/n");
WSACleanup();
return 0;
}
printf("Winsock start...\n");
struct sockaddr_in local;
struct sockaddr_in client;
int len = sizeof(client);
local.sin_family = AF_INET;
local.sin_port = htons(12345); //the port need listened
local.sin_addr.s_addr = INADDR_ANY; //localhost
socket1 = socket(AF_INET,SOCK_DGRAM,0);
bind(socket1,(struct sockaddr*)&local,sizeof(local));
while (1)
{
char buffer[1024] = "/0";
printf("waiting message from the clients...\n");
if(recvfrom(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&client,&len) != SOCKET_ERROR)
{
//pthread_mutex_lock (&lock);
printf("recevie message: [%s]---%s\n",inet_ntoa(client.sin_addr),buffer);
//send sth to the client
sendto(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&client,len);
printf("id=%d\n",pthread_self());
// pthread_mutex_unlock (&lock);
}
}
closesocket(socket1);
}
void *thread_socket2(void *v)
{
SOCKET socket2;
WSADATA wsaData;
int ErrorCode;
if(WSAStartup(MAKEWORD(2,1),&wsaData))// windows socket dll;
{
printf("Winsock initiate failed!!/n");
WSACleanup();
return 0;
}
printf("Winsock start...\n");
struct sockaddr_in local;
struct sockaddr_in client;
int len = sizeof(client);
local.sin_family = AF_INET;
local.sin_port = htons(12346); //the port need listened
local.sin_addr.s_addr = INADDR_ANY; //localhost
socket2 = socket(AF_INET,SOCK_DGRAM,0);
bind(socket2,(struct sockaddr*)&local,sizeof(local));
while (1)
{
char buffer[1024] = "/0";
printf("waiting message from the clients...\n");
if(recvfrom(socket2,buffer,sizeof(buffer),0,(struct sockaddr*)&client,&len) != SOCKET_ERROR)
{
pthread_mutex_lock (&lock);
printf("recevie message: [%s]---%s\n",inet_ntoa(client.sin_addr),buffer);
//send sth to the client
sendto(socket2,buffer,sizeof(buffer),0,(struct sockaddr*)&client,len);
printf("id=%d\n",pthread_self());
pthread_mutex_unlock (&lock);
}
}
closesocket(socket2);
}
int main(int argc, char *argv[])
{
pthread_mutex_init(&lock, NULL);//互斥鎖
pthread_create(&threads[0], NULL, thread_socket1, NULL);//線程創建
pthread_create(&threads[1], NULL, thread_socket2, NULL);
system("PAUSE");
pthread_mutex_destroy(&lock);
pthread_exit(NULL);
return 0;
}
#include
#include
#include
#include
#include"sqlite3.h"
char copy[1024]="\0";
char ID[1024]="_0356608";
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i2015-05-13 17:56
清晰明瞭!!感謝分享:D