> Please tell me how it is possible ?
You're returning a pointer to a local variable.
So when the function returns, the local variable disappears, and the pointer is invalid.
Note that this does NOT GUARANTEE that your code will break. With undefined behaviour, absolutely anything can happen, including producing the expected answer (always a tricky one this, because it makes you think you're right, when in fact you're wrong).
> that program works prefectly in Dev C++ but not in Turbo C++ compiler(Ver 3.0).
Which is why it's best to learn the language to decide what is correct (or not).
Trying to learn C++ by observing the behaviour of a range of compilers is no way to go.
Essentially, you should be able to look at that code, see that a pointer to a local variable is being returned and decide that it is wrong without actually trying it.
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Offline 7,164 posts
since Dec 2005