If you return a constant string (which in this case you are), your return value should be const char* . If you need to return a mutable string, there are three ways to do this, in the general case:
Force the caller to pre-select the line
int get_string (char c, char* outstr, size_t outstr_len) {
This is the most idiomatic element in C, but it requires most of the work to work.
Highlight Caller Name String
char* get_string (char c) { char* str = malloc();
Note that this method is relatively slow, and you will need to document that the responsibility of the caller is to free the returned string and that it should be freed using free (as opposed to delete[] ). You can use new[] and delete[] instead of malloc and free here, but I assume that the only reason you do this is to interact with the C code.
Highlight static buffer
char* get_string (char c) { static char* str[];
Here you must document how large the returned string is and that the caller does not release it. The main drawback here is that subsequent calls to get_string will discard previously returned rows.
Ed: Hm, markdown is not like mixed code and lists
Tyler McHenry Jul 24 '10 at 3:31
source share