c - Pointer to function vs global variable -
new ee little software experience here. have read many questions on site on last couple years, first question/post. haven't quite found answer one.
i know difference/motivation between having function modify global variable within body (not passing parameter), , between passing address of variable.
here example of each make more clear. let's i'm declaring functions "peripheral.c" (with proper prototypes in "peripheral.h", , using them in "implementation.c"
method 1:
//peripheral.c //macros, includes, etc void function(*x){ //modify x } .
//implementation.c #include "peripheral.h" static uint8 var; function(&var); //this end modifying var method 2:
//peripheral.c //macros, includes, etc void function(void){ //modify x } .
//implementation.c #include "peripheral.h" static uint8 x; function(); //this modify x is motivation avoid using "global" variable? (also, global if has file scope?)
hopefully question makes sense. thanks
the function receives parameter pointing variable more general. can used modify global, local or indeed variable. function modifies global can task , task only.
which preferred depends entirely on context. 1 approach better, other. it's not possible definitively 1 approach better other.
as whether global variable global, global in sense there 1 single instance of variable in process.
Comments
Post a Comment