Is it good/correct way to do like this for a robust C program
//File1 => Module1.h
static int Fun(int);
struct{
int (*pFn)(int)
}Interface;
//File2 => Module1.c
static int Fun(int){
//do something
}
Interface* Init(void)
{
Interface *pInterface = malloc(sizeof(Interface));
pInterface->pFn = Fun;
return pInterface;
}
//File 3 => Module2.c
#include"Module1.h"
main()
{
Interface *pInterface1 = Init();
pInterface1->pFn(5);
}
My intention is to make each module expose an interface...
Questions:
This is more idiomatic for a dynamically loaded module. (Or something along these lines.)
Usually an interface between two source files is defined using extern
functions and objects which are accessed directly. You would need to make a case for doing anything more sophisticated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With