your fun() is returning a local pointer from its internal stack. This stack got deallocted, so it is invalid.
I would do it this way:
int*fun(){
int *i= new int;
*i = 20;
return i;
}
Important: the returned int* must get freed like that
int *p = fun();
printf("%d",*p);delete p;