Thread: I need to transform a local variable into a global variable

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    10

    Question I need to transform a local variable into a global variable

    I need to transform a local variable into a global variable so I can use it in one of my functions.
    I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop).
    Example to visualize better:
    Code:
    while(condition)
    {
            function(parameter1, parameter2);
            count = count + 1;
    }
    printf("%d\n", count);
    So, I need to transform the final value of "count" into a global variable. Can I do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by eduarda2
    I need to transform a local variable into a global variable so I can use it in one of my functions.
    No, you don't.

    Quote Originally Posted by eduarda2
    I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop).
    Therefore you should pass a pointer to the variable or get the function to return the updated count.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    10
    "but I CAN'T do this"

    sorry for the typo...
    you can see that i can't do it in the example

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, that was obvious from the context, that's why I answered as if you wrote "can't" rather than "can".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    laserlight is saying you have NOT given a valid reason why you need the variable to be Global.

    laserlight gave two other choices.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by eduarda2 View Post
    ... this variable counts how many times the while loop does (so the final value is outside the loop).
    Is "count" only counting the number of passes through the loop, and therefore how many times the function is called?

    The code already does that:
    Code:
    while(condition)
    {
            function(parameter1, parameter2);
            count = count + 1;
    }
    printf("%d\n", count);
    "count" is already visible everywhere in that example.

    Or do you need to do something more with "count" inside the function?

    -
    Last edited by megafiddle; 10-25-2014 at 10:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string declaration as global vs a main local variable...
    By Jim Iwannou in forum C Programming
    Replies: 4
    Last Post: 11-14-2013, 03:12 AM
  2. Replies: 4
    Last Post: 05-25-2011, 10:33 PM
  3. From global to local variable
    By urban1990 in forum C Programming
    Replies: 11
    Last Post: 10-12-2010, 11:24 PM
  4. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM