I would like to provide a read-only modular variable for client modules. A few solutions:
1 . The most common:
static int a;
int get_a(void)
{
return a;
}
int get_a(void);
This allows you to use one function for each variable, one function call (I think both runtime and readability), and one copy for each read. Assuming no optimizer linker.
2 . Another solution:
static int _a;
const int * const a = &_a;
extern const int * const a;
int read_variable = *a;
*a = 5;
I like it, except that the client needs to read the contents of the pointer. In addition, for each variable is required for read-only pointer extern constto const.
3. , , , extern struct. module_name->a , .
4. get_a(void). - , .
:
, - , , , .