Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design for C program [closed]

Tags:

c

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:

  1. Is it good to write a C code like above to expose an interface... ??
  2. What better ways are available for exposing interface??
  3. Are there any references for design principles for C programming (not C++) ??
like image 613
user2706540 Avatar asked Oct 10 '13 09:10

user2706540


1 Answers

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.

like image 179
Potatoswatter Avatar answered Sep 19 '22 00:09

Potatoswatter