已有下面代码
#include <stdio.h>
typedef struct Meas_s Meas_t;
struct Meas_s {
double value;
void (*dispFunc)(const Meas_t*);
};
写一个函数 void meas_print(const Meas_t* meas) ,它调用在 meas 中指向的函数,以 meas 作为参数。
测试代码1:
void disp(const Meas_t* meas)
{
printf("%.3f m/s\n", meas->value);
}
Meas_t meas = {2.7777, &disp};
meas_print(&meas);
输出结果1:
2.778 m/s
测试代码2
void disp1(const Meas_t* meas)
{
printf("%.3f m/s\n", meas->value);
}
void disp2(const Meas_t* meas)
{
printf("Speed: %.1f metres per second\n", meas->value);
}
Meas_t meas1 = {2.7777, &disp1};
meas_print(&meas1);
Meas_t meas2 = {2.7777, &disp2};
meas_print(&meas2);
输出结果2:
2.778 m/s
Speed: 2.8 metres per second
参考答案:搜微信公众号【有趣理工男】回复:260LM5Q13
|