returning char addresses

May 7, 2013 at 2:12am
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 May 7, 2013 at 2:22am
May 7, 2013 at 2:24am
String literal constants are available globally from the string table, they don't leak memory or get destroyed with the stack.
May 7, 2013 at 2:25am
Put the code in between the code tags html symbols(whatever it is called).

Then why don't you change the return type?

Aceix.
May 7, 2013 at 2:26am
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?
May 7, 2013 at 2:36am
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 May 7, 2013 at 3:36am
May 7, 2013 at 3:33am
yep, @ jidder
Topic archived. No new replies allowed.