returning char addresses

I came across this simple member function and Im confused this function returns and character address how can it return the character string Base?


const char* GetName() { return "Base"; }
Last edited on
String literal constants are available globally from the string table, they don't leak memory or get destroyed with the stack.
Put the code in between the code tags html symbols(whatever it is called).

Then why don't you change the return type?

Aceix.
Aceix wrote:
Put the code in between the code tags html symbols(whatever it is called).
It's such a small snippet, he doesn't need to bother with it.
Aceix wrote:
Then why don't you change the return type?
The return type is correct, what are you referring to?
It is because it is returning a pointer.
The pointer points to a location like "0x348342" or whatever.
If the pointer points to a string literal it reads all subsequent data until it reaches the null terminator.
This means it doesn't actually return "Base" just the location.
Last edited on
yep, @ jidder
Topic archived. No new replies allowed.