-
modular c programming
hi;
i have a project with many modules. my questions are :
-if functions of module_superior call functions of module_inferior can i access global variable of module_superior in module_inferior(the superior module in an inferior module) by decalaring them as extern?
-if functions of module_superior call functions of module_inferior can i define a global variable in module_inferior then modify it in module_superior then read it in module_inferior?
which solution is the best?
thanks
-
The _BEST_ approach is to avoid global variables as much as possible.
Pass structures between different modules (as pointer to the struct, generally).
--
Mats
-
thx for reply but i haven't other choice i must use global variables.so which solution seems to work better?
-
That is a very rare case. There is no standard for where you should place your global variables. I would probably put them in the "superior" module and have extern declarations in a header file.
--
Mats
-
thanks.here's an example:
Code:
//module sup
#include "module_inf.h"
int var;
//modify var
void modif_var(void){
var=0;
//call of a function of module1 which reads var and do other instructions
module1_func();
}
///////////////////////////
//module inf
extern int var;
void module1_func(void){
read(var);//read var
}
sould this work?
-
Yes. I would, however recommend the use of an include file to declare the extern for var.
--
Mats