How to avoid globals in C?

as a newbie, I read everywhere to avoid overuse of global variables. Well, how to do it? My low skill doesn't work. I'm finishing the transfer of a ton of structures, and it's harder to read than using global variables. Argh is my code a mess any good book / article recommendations that are guided by this problem / structure structure of the application?

+3
source share
5 answers

Depending on what your variables do, a global scope may be a better scope. (Think that flags signal an interrupt and should be processed at a convenient time in the middle of the calculation cycle.)

, ( , , ); - . .

, "" . , - , , .

, , , , - , . , , , -.

, , Code Complete, 2nd edition. , , . , .

: Advanced Programming in Unix Environment, 2nd Edition , .

, , , . , , , . .:)

+8

Yourdon Constantine. ( !), , .

+1

, :

  • , const:

    struct my_struct;

    const my_struct * GetMyStruct (void) const;

  • , . , :

    static mystruct myStructInstance;

0

"" , , , , "" . , , :

#define current_filename   context->current_filename
#define option_flags       context->option_flags

... , , , , .. . , , .

0

@PeterK , ​​ C , / , , ( ).

3- 1 . , , , , :

  • ex. (sInverterState, sButtonsState, sInverterParameters ..).
  • If I write a UI menu, I can use static variables in the C file and do not care about passing structures when I have only 1 LCD. I do not want it to look like GTK ++.
  • Writing a re-code is not yet for me and its overdoing for this purpose.
  • Get the right IT education.

I can end up with a lot of globals, but at least they are well-packaged and readable.

0
source

Source: https://habr.com/ru/post/1757629/


All Articles