Removing (or rather conditionally attach) const modifier using macros in C -
I am dealing with the following issue in C: I use global variables to define some global parameters in my code. I would like to have such a global variable stable, even if they should be started on a regular basis, which read their values from the inlet data file. In short, I'm looking for a good way to delete "Cost" during variable initialization (I think that there seems to be no reason for My attitude: I have a main header file in which my global variable ( I have a file "get_global_params.c", which implements the initialization of N, which looks as "N <"> "(as in" main_header.h " After defining and the related header file "get_global_params.h" I hope my explanation was quite clear. Thanks for any feedback. Just place the globals in a separate file. Globl.h: init_globl.h: globl.c Now whatever file you need to access that functionality, enterGlobalL. Can also be started by adding, everyone can directly reach globals including the globe. H, and using the notation const_cast in C ++) I used to come up with a pattern based on macros to do this, as illustrated below. It works fine, but I have the following questions whether any hidden faults or down Have seen potential threats in the process?
int n ), Such as
/ * main_header.h * / #ifdef global_params_reader #define __TYPE__QUAL__ #else #define __TYPE__QUAL__ const #endif __TYPE__QUAL__ int N;
global_params_reader
/ * get_global_params.c * / #define global_params_reader #include get_global_params.h void get_global_params (char * filename) {N = .. .; // some functions that read the value of N from the datafile "filename" and give it returns}
/ * get_global_params.h * / #include "main_header.h" zero get_global_params (char * filename); Finally, I have the main C, which looks at N as "
const n " (as in
without defining it in " main_header.h ") global_params_reader ):
/ * main.c * / #include" main_header.h "#include" get_global_params.h "Ent Main (int argc , Char ** argv) // // Input data file installation // // // get started // get_global_params (datafile); // do not work with n // ...}
Stretch globals {int n; // ...}; Exterior set Strobe Globals * Cons Globals;
init_globals (/ * infinity * /);
#include globl.h #include init_globl.h static strat globals _ globals; Constrast Globals * Cons Globals = & amp; _globals; Init_globals (/ * Init Params * /) {// Start _globals; // ...}
globals-> N .
Comments
Post a Comment