Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 19th, 2005, 12:38 PM   #1
Ricky205
Newbie
 
Join Date: May 2005
Posts: 4
Rep Power: 0 Ricky205 is on a distinguished road
C++ Global Variables question

Hey, Im learning about global variables and i have a question concerning the example code below. Now I know that count is declared outside any function therefore can be used by any, but the value of count is given in the int main() function, so when it calls the second function how is it that it still works ? or is it that it doesnt matter where the value given is located as long as the variable is declared globaly or as long as there isnt a local variable with the same name that will interfere ?

thanks in advanced

#include <iostream>
using namespace std;

void func1();

int count; // this is a global variable 

int main()
{
  int i; // this is a local variable 

  for(i=0; i<10; i++) {
    count = i * 2;
    func1();
  }

  return 0;
}

void func1()
{
  cout << "count: " << count; // access global count
  cout << '\n'; // output a newline
}
Ricky205 is offline   Reply With Quote
Old May 19th, 2005, 1:31 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
That will work fine, but yes, you're right - you can't declare a local variable if you have a global variable with the same name.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 19th, 2005, 2:06 PM   #3
Ricky205
Newbie
 
Join Date: May 2005
Posts: 4
Rep Power: 0 Ricky205 is on a distinguished road
ok thanks , now I got a second question about it ..
what if u were to declare a global variable named var for instance. Now lets say you have 2 functions .. can u use var in both functions with each function having a different value for it .. say :

outside all functions:
int var;

inside the first function :
var = 10;

inside the second function :
var = 50;
Ricky205 is offline   Reply With Quote
Old May 19th, 2005, 4:33 PM   #4
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
Yes, variables in different functions don't clash with each other.

If you have a global variable of a given name, for example foo, you can create a local one with the same name if you please.

int foo;

int bar() {
  int foo;
}

Code inside the function, that is in the same scope as the local variable foo, will refer to the local variable when it refers to the variable called foo. Code outside it, in other functions, will refer to the global foo, unless they define a variable called foo of their own.

--
I just realised I kind of missed the point of what you were asking there. If you declared the global variable var, you could use it in two different functions, and they'd both be referring to the same variable. So in func1, if you set var to 10, then call func2, func2 will see the value 10 in it. If it now sets it to 20 and returns, and you then call func1, func1 sees the new value.

A global variable is one name and one storage location and it's shared throughout the program. It can only take one value at a time, and any function that assigns a value to the global variable will change the value any other function sees.

Last edited by mackenga; May 19th, 2005 at 4:35 PM.
mackenga is offline   Reply With Quote
Old May 20th, 2005, 3:31 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
I am not sure about c++ but as a rule of thumb when i am using java i tend to use this.<variable> for gloadls and then just the name of the variable for the local one.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old May 20th, 2005, 3:14 PM   #6
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
There aren't any global variables in Java. As well as not being inside a function, global variables (in languages that have them) are not inside anything else.
mackenga is offline   Reply With Quote
Old May 21st, 2005, 10:25 AM   #7
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
well its close enough to a global variable.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old May 21st, 2005, 11:12 AM   #8
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
You can use the same name over and over in functions and blocks. C/C++ has a means of hiding the hierarchically higher variables.
For instance:
int a;

void f () { int a; { int a; { int a; } } }
Is valid code. We have four diferent instances of the same variables (or four diferent variables with the same name). In each case the program sees only the most recently declared version. The global variable is accessible by the resolution operator (i.e. ::a). Though you cannot acces hierarchically higher local variables (such as the second "a" from the third block).


Global versions of variables can be accessed through ::my_var where my_var is the name of the variable.
Aphex_Twin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC