I\'m working with some code that widely uses the idiom of returning a pointer to a static local variable. eg:
char* const GetString()
{
static char sTest[
Yes, this is used frequently to return the text portion of some lookup, i.e. to translate some error number into a human friendly string.
Its wise to do this in instances where you'd:
fprintf(stderr, "Error was %s\n", my_string_to_error(error_code));
If my_string_to_error()
returned an allocated string, your program would leak given the above (very) common usage of such a function.
char const *foo_error(...)
{
return "Mary Poppins";
}
... is also OK, some brain dead compilers might want you to cast it though.
Just watch strings in this fashion, don't return a book :)