I have 4 files:
main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
int main() {
struct Fun *fun = (struct Fun*)malloc(sizeof(struct Fun));
fun->a = 2;
fun->b = 12;
fun->Func = Plus();
int result = fun->Func(fun, 8);
printf("%d\n", result);
return 0; }
main.h
#ifndef MAN_H_
#define MAN_H_
struct Fun {
int a;
int b;
int (*Func)(struct Fun *x,int y);
};
header.c
#include "header.h"
int Plus(struct Fun *x, int y) {
return x->a * x->b + y; };
header.h
#ifndef HEADER_H_
#define HEADER_H_
#include "man.h"
#endif /* HEADER_H_ */
when I build, I get a warning:
../main.c:12:5: warning: implicit declaration of function ‘Plus’ [-Wimplicit-function-declaration] ../main.c:12:15: warning: assignment makes pointer from integer without a cast [enabled by default]
if I run, it has no result.
But when I put all the code to main.c and edit fun->Func = Plus(); to fun->Func = Plus; it works fine: no warning, and the result is 32.
malloc.