Why C++ returns valid data from curly brackets scope inside function if i point data from this scope to a pointer that was declared outside these curly brackets scope?
Example:
int *pInt;
// why *pInt is still "valid" ?
{
int x = 134;
pInt = &x;
}
cout << *pInt << endl;
// *pInt returns 134
Another example:
// or why *pChar is still "valid" ?
char *pChar;
{
char cArr[] = { 'Y', 'o', '!', '\0' };
pChar = cArr;
}
cout << pChar << endl;
// pChar returns 'Yo!'
I do not understand this, because in this case:
int * invalid_pointer(){
int x = 134;
return &x;
}
int main(void){
int *pInt = invalid_pointer();
cout << *pInt << endl;
return 0;
}
*pInt wil be '2079168904', which is invalid value, and that is correct behavior in such case, and i expected the same behavior with curly brackets case.
Please explain why these two cases are different. Thanks!