S
Siam
Hi all...
What's the best way to return some string from a function (without
using the string class). Would the following work:
char* getString( )
{
return "TheString";
}
I've seen this in code in a lot of places, but I would have thought the
string literal in the function would be allocated on the stack, a
pointer to the string will be returned, and the block of memory holding
the string would be deallocated when the function returns... Would it
make a difference if the return type was const char* ?
If this doesn't work, the only other way I can think of is to either
dynamically allocate a new string in the function (in which case it
wouldn't be clear to the user to delete it), or to make them pass a
reference to a string and strcpy "TheString" into it (which I find
annoying)... Is there any other way?
What's the best way to return some string from a function (without
using the string class). Would the following work:
char* getString( )
{
return "TheString";
}
I've seen this in code in a lot of places, but I would have thought the
string literal in the function would be allocated on the stack, a
pointer to the string will be returned, and the block of memory holding
the string would be deallocated when the function returns... Would it
make a difference if the return type was const char* ?
If this doesn't work, the only other way I can think of is to either
dynamically allocate a new string in the function (in which case it
wouldn't be clear to the user to delete it), or to make them pass a
reference to a string and strcpy "TheString" into it (which I find
annoying)... Is there any other way?