Yep, the output is undefined because j is local to fun() and its address is no longer valid when the function returns.

So if the program needs to return the address of a new integer then you should create a new one on the heap with new or malloc, and remember somewhere in the program to delete/free it when you've finished with it, otherwise you get a memory leak.