c语言函数指针的多种赋值方法
提供框架模型,具体代码缩略,适合功能模块包装,例子程序为c语言基于TCP的图片发送
1、mdu_TcpClient.h中:
定义结构体
typedef struct
{
int (*init)(char *szIpaddr, char *wTcpPort); //初始化tcp客户端
int (*sendData)(unsigned int ntskId, unsigned int nsize, long long ntimestamp, char *pStream); //发送数据
void (*close)(void);
}MDU_TCPCLIENT_ARG_T;
2、mdu_TcpClient.c中:
各个子函数的定义,具体内容缩略
int initiation(char *szIpaddr, char *wTcpPort)
{
...
}
int sendPack(unsigned int ntskId,unsigned int nsize, long long ntimestamp, char *pStream)
{
...
}
void close_tcpclient(void)
{
...
}
结构体函数指针赋值
MDU_TCPCLIENT_ARG_T mdu_tcpclient_arg = {
.init = initiation,
.sendData = sendPack,
.close = close_tcpclient,
};
或者如下
MDU_TCPCLIENT_ARG_T mdu_tcpclient_arg = {
initiation,
sendPack,
close_tcpclient,
};
3、client_main.c中
主函数调用
int main(int argc, char *argv[])
{
...
//函数指针调用
if(mdu_tcpclient_arg.sendData(12,1024,1230*1230, PicName)!=1)
{
...
}
...
...
}
END