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 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?

My attitude:

I have a main header file in which my global variable ( int n ), Such as

  / * main_header.h * / #ifdef global_params_reader #define __TYPE__QUAL__ #else #define __TYPE__QUAL__ const #endif __TYPE__QUAL__ int N;   

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 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}

and the related header file "get_global_params.h"

  / * 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 // ...}   

I hope my explanation was quite clear. Thanks for any feedback.

Just place the globals in a separate file.

Globl.h: Stretch globals {int n; // ...}; Exterior set Strobe Globals * Cons Globals;

init_globl.h:

  init_globals (/ * infinity * /);   

globl.c

  #include globl.h #include init_globl.h static strat globals _ globals; Constrast Globals * Cons Globals = & amp; _globals; Init_globals (/ * Init Params * /) {// Start _globals; // ...}   

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 globals-> N .

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -