Why is this allowed ?

Started by
6 comments, last by Metzler 21 years, 7 months ago
I didnt find a more applying head line. What i mean, is the following :

int i = 1;

void bla()
{
	int i = 0;
}

void main(void)
{

}
 
The two declaration of int i make me think. Is this ok ?? I know, that the first one is global and the second a local, but cant they "crash" each other ? I mean, in the function of bla, i can access a global int i, but even a local int i... I am... confused [Edit: GDNet tags are usually lowercase.] [edited by - Oluseyi on August 26, 2002 4:39:51 PM]
Advertisement
It works like this:

If you in a function (such as bla()) declare a local variable with the same name as a global one, the local will always be used instead of the global one in that function (or scope).
However, if you would want to access the global i, after you''ve declared a local i, you can use the :: operator. Like this:


  int i;  //Global ifunction bla(){int i;  //Local ii = 5;    //Set local i to 5::i = 4;  //Set global i to 4}  


/John
/John
THX !
Hi,

When you declare a local variable that has the same name as the global variable. The variable that you declared locally will be used instead of the one declared globally until the local variable goes out of scope. Yes this is totally legal .

As an example.


  int i = 0;int main(){i++; // The global i is now == 2;int i = 5;i++; /* This doesn''t make the global i == 3, it instead makes the local i == 6*/}  


Try it out yourself, print the values if you want using cout or printf.

Note that this will only compile under C++ not C, since I didn''t declare the local variable at the beginning of the block.

Hope that helps ,

Destroyer

perfectly valid code in C++, the only difference is the scope in which they exist.... for example :

consider this :


  int i;namespace moo{    int i;};namespace baa{    int i;};// moo::i is not the same as baa::i which is not the same as i// now with the bla function you declared....void bla(){    // this is defined within the scope of bla,     int i;    // calling this affects the local i above    i = 5;    // calling this affects the global i    ::i = 5;    // and calling this affects the i declared in the moo namespace    moo::i = 5;}// you would however start having headaches if you did something like :using namespace moo;// which i does this refer to ?i = 5;  


In this case the local variable takes precedence over the global variable. Therefore if you use the variable ''i'' within the function in which it was defined, you will get that value, if you use it elsewhere however, you will get the global value. This may seem confusing to you because the variables have the same names, but when the code is compiled the compiler will created temporary variable names for these two variables that are different. However, situations like this are why it is good programming convention not to have 2 different variables with the same name.

Hope I helped,
Rob
** WOW - lots of people answered in a very short space of time!! When I started writing, there were no replies. When I finished there were already 5!!! **


It's all to do with scope rules. Basically within any set of curly braces {}, there cannot be multiple variables with the same names active at the same time. To get around this the more general variable is 'hidden' by the less general variable within that scope.

In the example you gave, the first int i (=1) has global scope, which is the most general scope. Any scope (set of curly braces) which does not have a more specific definition of i will use the global variable i.

Your function bla() has a more restricted scope and therefore the second declaration (int i = 0) is less general than the global int i. Note that there are still two variables, and the global int i still has value 1, it is just hidden by the more local variable i, which has value 0. When you exit function bla() the local int i (=0) ceases to exist and the global int i comes back into scope (still with value = 1).

So in summary (using prefix (g_) to denote global variable and (l_) to denote local variable):
[pre]
int (g_)i = 1;
// (g_)i = 1, (l_)i = undefined;

void bla(){
// now in local scope
// (g_)i = 1, (l_)i = undefined;
int i = 0;
// (g_)i = 1, (l_)i = 0.
// (g_)i is hidden by (l_)i and is inaccessible
}
// back in global scope
// (g_)i = 1, (l_)i = undefined;
void main(void){
// different local scope
// (g_)i = 1, (l_)i = undefined;
}
// back in global scope
// (g_)i = 1, (l_)i = undefined;
[/pre]

If you want to know more, do a search for "variable scoping rules".

Hope that helps

Enigma

[edited by - Enigma on August 26, 2002 4:50:19 PM]
THX @ all, you helped me !

This topic is closed to new replies.

Advertisement