Hi,
Can someone tell me how the following simple examples work? In the first one, 'i' goes out of scope after being assigned to a variable outside the block. How does the outer variable have a value after the block that 'i' is in ends? Does a copy of 'i' get assigned to the outer variable, so that when the local variable is destroyed, it doesn't affect the outer variable?
In the second example, the inner ariable is an array, and it's address is assigned to the outer variable. When the inner array goes out of scope, how does the outer variable still contain a valid address?
Code:#include<iostream>
using namespace std;
int main()
{
//test1 ***
int outerA;
if(true)
{
int i = 10;
outerA = i;
}
cout<<outerA<<endl;
//******************
//test2 ***
int* outerB;
if(true)
{
int my_array[3] = {1, 2, 3};
outerB = my_array;
}
cout<<outerB[0]<<endl;
//*******************
return 0;
}