Consider this piece of code -
#include<bits/stdc++.h>
using namespace std;
struct node{
char symbol;
node * left = NULL;
node * right = NULL;
};
node * somefn(node *ptr){
node temp;
node temp2;
temp.left = &temp2;
temp.symbol = 'b';
ptr = &temp;
return ptr;
}
int main(){
node *ptr;
ptr = somefn(ptr);
cout<<(ptr->symbol)<< "\n"; // this statement
if(ptr->left->left != NULL) cout<<"this shldnt print";
return 0;
}
When I execute this keeping the first cout statement I get the output -
b
this shldnt print
However, when remove the first cout statement, nothing gets printed(the second cout neither). Why this inconsistent behavior? I am using gcc version 5.4.0. I even tried this code on online ide but the same thing happened there too.