Well, they say variables that are declared inside a function or a block are said to have local scope i.e. these variables can only be used within the function or block in which these are declared.
But, after running following program, I got strange result.
int* ptr = NULL;
void Func()
{
int x = 10;
ptr = &x;
}
int main()
{
Func();
cout << *ptr << "\n"; // prints 10
*ptr = 555;
cout << *ptr << "\n"; // prints 555
return 0;
}
I accessed the variable "x" (declared in Func()) in main(). I was able to read and write to this variable in main(). I ran the code on Visual Studio 2015.
What's going on?