String literals are stored statically and of course the static int i
is static too. You can return pointers to static variables from functions because they are not local variables, and they are not destroyed when you exit the function as stack allocated variables do. On the other hand, your first example should return a const char *
,
From the C++ standard section lex.string:
A string literal ... has type "array of n const char" and static storage duration (basic.stc), where n is the size of the string as defined below, and is initialized with the given characters...