64,690
社区成员
发帖
与我相关
我的任务
分享
typedef struct
{
int c;
int d;
int e;
}UNI;
typedef int (*fun1)(int a,int b);
typedef int (*fun2)(UNI in);
void register_callback(fun1 f1_callback)
{
//要求1. 调用register_callback_hw
//要求2. 最终callback会调到print函数实现打印,实现和register_callback_hw同样
//功能
//要求3. 不能使用全局变量
}
void register_callback_hw(fun2 f2_callback)
{
UNI in;
in.c = 3;
in.d = 2;
f2_callback(in);
}
int print_struct(UNI in)
{
printf("a= %d b= %d a*b= %d\n",in.c,in.d,(in.c) * (in.d));
}
int print(int a,int b)
{
printf("a= %d b= %d a*b= %d\n",a,b,a*b);
return 0;
}
int main()
{
register_callback_hw(print_struct);
system("pause");
}
#include <iostream>
using namespace std;
typedef struct
{
int c;
int d;
int e;
}UNI;
typedef int (*fun1)(int a,int b);
typedef int (*fun2)(UNI in);
int print(int a,int b)
{
printf("print: a= %d b= %d a*b= %d\n",a,b,a*b);
return 0;
}
void register_callback_hw(fun2 f2_callback)
{
UNI in;
in.c = 3;
in.d = 2;
f2_callback(in);
}
void register_callback(fun1 f1_callback)
{
//要求1. 调用register_callback_hw
//要求2. 最终callback会调到print函数实现打印,实现和register_callback_hw同样
//功能
//要求3. 不能使用全局变量
fun2 tmp = (fun2)f1_callback;
register_callback_hw(tmp);
}
int print_struct(UNI in)
{
printf("print_struct:a= %d b= %d a*b= %d\n",in.c,in.d,(in.c) * (in.d));
return 0;
}
int main()
{
register_callback_hw(print_struct);
register_callback(print);
system("pause");
}
理解函数指针强转,看汇编,call的时候, 少push一个形参