(Objective C Code)
int i=5;
{
int i=i;
NSLog(@"Inside Scope: %i",i);
}
NSLog(@"Outside Scope: %i",i);
Prints:
3385904 (Garbage)
5
replacing int i = i; with int i= 10; prints correctly... (Inside the scope's i)
Such as:
10
5
And (This code alone)
int i=i;
Compiles, but segfaults immediately.
How are any of these syntax's valid? What use are they, or are they compiler bugs that should have been caught earlier?
Is there any situation where it is necessary for using the same variable names inside a new scope under a new type, and how would you differentiate?
My only thoughts is could be the for() loop, as the compiler would be upset you're redefining int i; twice if you have two loops.
int i = i;, but clearly it's invalid. Defining a newiinside{}is perfectly legit, however, and occasionally useful (though potentially confusing and bug-prone).