#include
#include
#include
#define uint8_t unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned long
#define uint64_t unsigned long long
#define CMD_TXT_LEN 30
#define CMD_NUM_MAX 100
typedef struct
{
char cmdString[CMD_TXT_LEN];
long (*cmd_func)(void *param);
int param_len;
} Command_Tab;
typedef struct
{
int x;
int y;
} TagPoint;
typedef struct
{
int x;
int y;
int width;
int height;
} TagMatrix;
typedef struct
{
char name[10];
uint8_t age;
float tall;
uint16_t score;
} TagStudent;
int StrCmp(char *buf,char *str)
{
int len = strlen( buf );
for(int i=0;ix,parm->y);
return 0;
}
long SetMatrix(void *arg)
{
TagMatrix *parm = (TagMatrix*)arg;
if(parm == NULL)
{
return -1;
}
printf("[SetMatrix], x=%d,y=%d,Width=%d,Height=%d.\n",parm->x,parm->y,parm->width,parm->height);
return 0;
}
long SetVersion(void *arg)
{
char *str = (char*)arg;
printf("[SetVersion],VER=%s\n",str);
return 0;
}
long StudentShow(void *arg)
{
TagStudent *parm = (TagStudent*)arg;
if(parm == NULL)
{
return -1;
}
printf("[StudentShow],name=%s,age=%d,score=%d,tall=%.1f\n",
parm->name,parm->age,parm->score,parm->tall);
return 0;
}
long Delay(void *arg)
{
printf("[Delay]\n");
return 0;
}
static const Command_Tab functionList[CMD_NUM_MAX]=
{
{"SetPoint",SetPoint,sizeof(TagPoint)},
{"SetMatrix",SetMatrix,sizeof(TagMatrix)},
{"SetVersion",SetVersion,10},
{"StudentShow",StudentShow,sizeof(TagStudent)},
{"Delay",Delay,0},
{(char*)NULL,NULL,0}
};
long DataAnalysis(char *buf,void *param)
{
int result=-1;
long (*userFunc)(char *data);
char *str;
for(int i=0;i
#include
#define uint8_t unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int
typedef struct
{
uint8_t x;
uint8_t y;
} ParamFunc1;
typedef struct
{
uint16_t code;
} RetFunc1;
typedef struct
{
char name[10];
uint8_t age;
} ParamFunc2;
typedef struct
{
uint32_t timestamp;
} RetFunc2;
long fun1(long wp, long lp)
{
static RetFunc1 ret;
ParamFunc1 *pm = (ParamFunc1 *)wp;
printf("fun1 called. x=%d,y=%d.\n", pm->x, pm->y);
ret.code = 0x1abb;
return (long)&ret;
}
long fun2(long wp, long lp)
{
static RetFunc2 ret;
ParamFunc2 *pm = (ParamFunc2 *)wp;
printf("fun2 called. name=%s,age=%d.\n", pm->name, pm->age);
ret.timestamp = 10086;
return (long)&ret;
}
//指针函数表
long *func_tab[] =
{&fun1, &fun2};
int main()
{
// 定义函数指针,并作赋值测试
long (*fp)(long wp, long lp);
ParamFunc1 pm1 = {10, 20};
ParamFunc2 pm2 = {"Jack", 35};
RetFunc1* rt1;
RetFunc2* rt2;
for (size_t i = 0; i < 2; i++)
{
printf("fun_tab[%d] addr=0x%p.\n",i,func_tab[i]);
}
fp = func_tab[0];
rt1 = (RetFunc1*)fp(&pm1, 0);
printf("fun1:ret, code = %x.\n", rt1->code);
fp = func_tab[1];
rt2 = (RetFunc2*)fp(&pm2, 0);
printf("fun2:ret, timestamp = %d.\n", rt2->timestamp);
return 0;
}
运行输出:
fun1 called. x=10,y=20.
fun1:ret, code = 1abb.
fun2 called. name=Jack,age=35.
fun2:ret, timestamp = 10086.