Thread: Confusion with global variables

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Confusion with global variables

    I am getting strict orders to limit the number of global variables to be used. So i am trying to change the logic if possible. One such scenario is the application gives a request to transmit a group of messages and it is asynchronous. There is a cyclic task which will be doing this job of transmission. But how does it know that an application is requesting for transmission without global variables. Is it possible? Please advise.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    If you must change your "global" variable inside a function, it is always possible to pass the pointer to a local variable from the caller. For example:
    Code:
    void do_something(int *);
    
    int main( void )
    {
      int state = 0;
    
      do_something( &state );
      // now 'state' is 1.
      ...  
    }
    
    void do_something( int *statep )
    {
       // change *statep if you must!
       *statep = 1;
    }
    Technically, this 'state' variable isn't global...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some confusion about define global variable in c++
    By ztdep in forum C++ Programming
    Replies: 7
    Last Post: 12-29-2010, 04:50 AM
  2. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  3. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  4. global variable confusion
    By agentsmith in forum C Programming
    Replies: 10
    Last Post: 01-30-2008, 02:25 PM
  5. static variables confusion
    By Kleid-0 in forum C Programming
    Replies: 6
    Last Post: 01-08-2005, 06:11 PM

Tags for this Thread