>char *p = "india";
This is a pointer to a string literal. When you return p, you're returning the address of the string literal. A string literal has a lifetime beyond the function, so the memory isn't reclaimed and you don't get garbage. This is perfectly legal.
>char p[] = "india";
This is a string literal copied into an array. When you return p, you're returning the address of a local variable. A local variable is destroyed when the function returns, so you get garbage. This results in undefined behavior.
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Offline 11,807 posts
since Sep 2004