1 原因
看了open ssl代码里面很多在结构体里面放很多函数指针,然后再来调用,这样比较规范和统一
2 代码实现
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>typedef struct key_info
{int (*init)(struct key_info *info);int (*set_name)(struct key_info *info, const char *name);int (*get_name)(struct key_info *info, char name[], int size);pthread_mutex_t lock; char name[128];int (*fun)(struct key_info *info, int x, int y);
}KeyInfo;static int init(KeyInfo *info);
static int set_name(KeyInfo *info, const char *name);
static int get_name(KeyInfo *info, char name[], int size);
static int add(int x, int y);static int add(KeyInfo *info, int x, int y)
{return x + y;
}
static int init(KeyInfo *info)
{assert(info != NULL);memset(info->name, 0, sizeof(info->name));info->init = init;info->set_name = set_name;info->get_