This seems like a question that would have been asked before, but I couldn't find it. (If you do, please point me there and close this as a duplicate.)
I have C code which works something like this:
printf("some characters");
for (; n >= 0; n--)
{
printf(", %s", f(n, k));
}
printf("more characters");
for some variables n
and k
and function char* f(int n, int k)
which are not important here. (n
and k
are not constant.) I would like to convert the code above to a function which returns a char*
rather than simply using printf
to display it. (It will ultimately be printed but this lets me do some refactoring and is of course easier to test that way.) Of course I could just create the strings and copy them over character-by-character but surely there is a better (cleaner, faster, more idiomatic) way of doing this.
In my particular application f
is a very simple function, sort of a relative of itos
, but which is not cacheable in any meaningful way due to k
. But I welcome ideas which are good for that case as it may be useful to others in the future.