referencing a global variable from a function without passing it asan argument

Discussion in 'C Programming' started by iC and iC++, Nov 22, 2010.

  1. iC and iC++

    iC and iC++ Guest

    I have the following code snippet from my program that i am working on

    int running = 0;
    float var1, average_value;

    void is_process_running(void) {
    if (fabs(var1 - average_value) <= 0.001)
    {
    *(&running) = 0;
    }
    else
    *(&running) = 1;
    }

    var1 and average value get their value from somewhere else.

    is it possible to change the value of "is_running" by referencing to
    it with a pointer to its address i.e. <return *(&running)> with out
    passing it as an argument to the function or making it the return
    value of the function.

    Thanks,
     
    iC and iC++, Nov 22, 2010
    #1
    1. Advertising

  2. Re: referencing a global variable from a function without passing it as an argument

    "iC and iC++" <> writes:
    > I have the following code snippet from my program that i am working on
    >
    > int running = 0;
    > float var1, average_value;
    >
    > void is_process_running(void) {
    > if (fabs(var1 - average_value) <= 0.001)
    > {
    > *(&running) = 0;


    This is equivalent to
    running = 0;
    Why not just write that?

    > }
    > else
    > *(&running) = 1;


    And this is equivalent to
    running = 1;

    > }


    > var1 and average value get their value from somewhere else.
    >
    > is it possible to change the value of "is_running" by referencing to
    > it with a pointer to its address i.e. <return *(&running)> with out
    > passing it as an argument to the function or making it the return
    > value of the function.


    Assuming that you mean "running" rather than "is_running", yes, of
    course it's possible; that's what your code snippet does. (But it's a
    pointer to the object, not "a pointer to its address". A pointer value
    *is* an address.)

    Excessive use of globals variables is a bad idea. Without commenting on
    the references to var1 and average_value, your is_process_running()
    function should probably just return a value indicating whether the
    process is running or not (that's certainly what I'd expect given the
    name). For example:

    int is_process_running(void) {
    return fabs(var1 - average_value) > 0.001;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 22, 2010
    #2
    1. Advertising

  3. iC and iC++

    iC and iC++ Guest

    Re: referencing a global variable from a function without passing itas an argument


    >     int is_process_running(void) {
    >         return fabs(var1 - average_value) > 0.001;
    >     }
    >


    Thanks a lot. This is great. I am trying to limit the use of global
    variables as much as I can.
     
    iC and iC++, Nov 22, 2010
    #3
  4. Re: referencing a global variable from a function without passing it as an argument

    On Mon, 22 Nov 2010 12:37:39 -0800 (PST), "iC and iC++"
    <> wrote:

    >
    >>     int is_process_running(void) {
    >>         return fabs(var1 - average_value) > 0.001;
    >>     }
    >>

    >
    >Thanks a lot. This is great. I am trying to limit the use of global
    >variables as much as I can.


    Then the values to be processed by the function should be passed as
    arguments rather than store them in global variables referenced by the
    function directly.

    --
    Remove del for email
     
    Barry Schwarz, Nov 23, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    658
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,892
    Eric Sosman
    Jan 7, 2005
  3. zxo102
    Replies:
    2
    Views:
    1,837
    Piet van Oostrum
    Oct 13, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
  5. Bitswapper
    Replies:
    5
    Views:
    148
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page