In the code below, i expect tmp character array to be destroyed after f() returns and hence x should not be printed at all.
However, in the below function x get printed in main() but the for loop does not print the correct thing. Could someone explain this behavior. Here is the output.
abcdefg a b c d e f g abcdefg ?
k Y i
#include <iostream>
using namespace std; 
char* x;  
void f() 
{
    char tmp[100]= "abcdefg";   
    x = tmp; 
    cout << x << endl; 
    for(int i=0; i < 7; i++) 
        cout << x[i] << endl; 
}
int main() 
{
    f();
    cout << x << endl; 
    for(int i=0; i < 7; i++) 
        cout << x[i] << endl; 
}
 
     
     
     
     
     
    