#ifndef CALL_FUNCTION_H
#define CALL_FUNCTION_H
typedef int *callback_random(char *random,int len);
void set_callback(callback_random *cb);
int genrate_random(char *random,int len);
#endif
#include "call_function.h"
#include <stdio.h>
callback_random* pcall_rb = NULL;
void set_callback(callback_random* cb)
{
pcall_rb = cb;
}
static int default_callback(char* str, int len)
{
printf("default:%s\n", str);
return len;
}
int genrate_random(char* str, int len)
{
int res = 0;
if (pcall_rb == NULL)
{
res = default_callback(str, len);
}
else
{
res = pcall_rb(str, len);
}
return res;
}
#include "call_function.h"
#include <stdio.h>
int rand_num(char* str, int len)
{
printf("%s\n", str);
return rand()%len;
}
int main()
{
int res = 0;
set_callback(rand_num);
res = genrate_random("hello world", 20);
printf("%d\n", res);
return 0;
}