Returning a locally created const char*
This is well-formed, the returned pointer is valid and not dangled; because the string literal (i.e. "abc"
) has static storage duration and exists in the whole life of the program.
String literals have static storage duration, and thus exist in memory for the life of the program.
As you said when the function returns the local variable x
gets destroyed, but the string literal pointed to by it doesn't.