構造体で関数ポインタをどのように使用しますか? 具体的には、次の例では、プログラムはコンパイルされますが、実行時にクラッシュします。
ヘッダファイル内
#ifndef __FUNCTION_IN_STRUCT_H_
#define __FUNCTION_IN_STRUCT_H_
struct functionDaemon {
int id;
//double (*funcp); // function pointer
double (*fp)(double); // Function pointer
};
// #define NULL 'V'
#endif /* _FUNCTION_IN_STRUCT_H_ */
C ファイルでは:
#include <math.h>
#include <stdio.h>
#include "function_in_struct.h"
extern struct functionDaemon *ftnAgent;
void do_compute_sum (void) {
void* agent;
// struct functionDaemon *ftnAgent = (struct functionDaemon *) agent;
struct functionDaemon *ftnAgent;
double sum;
// Use 'sin()' as the pointed-to function
ftnAgent->fp = sin;
sum = compute_sum(ftnAgent->fp, 0.0, 1.0);
printf("sum(sin): %f\n", sum);
}
教えてください。