I am learning cpp for a month now and I always thought that variables inside functions, initialized on the stack, cannot be accessed from outside the function scope. However this doesn't seem to be the case when I use a global pointer to a member of the function like in the following case:
#include <iostream>
void fun(void); int *pInt;
int main(int argc, char **argv) {
pInt = 0;
fun();
std::cout << "*pInt = " << *pInt << std::endl;
return 0;
}
void fun(void) {
int a = 3;
pInt = &a;
}
Compiling and running this doesn't produce any error and indeed prints out the expected result. Why is this happening? Isn't "a" suppose to get out of scope (and thus its value) after the function fun is out of scope?