Your writeSpace function is not returning a new string (even though you've declared it that way) but is writing to the terminal directly. Since you call it as an argument in your printf call, writeSpace gets called first, prints its stuff, and then printf prints its stuff. You should do it like this:
char* writeSpace(int i)
{
char *ret = NULL;
asprintf(ret, " " + (30-i));
return ret;
}
Of course, this requires you to free the memory (otherwise you'd have a memory leak). You could do it like this:
char *spaces = writeSpace(10);
printf("%s%i", spaces, 42);
free(spaces);
An alternative is to use a static variable that the function itself cleans up on the next call:
char* writeSpace(int i)
{
static char *ret = NULL;
if (ret != NULL) free(ret);
asprintf(ret, " " + (30-i));
return ret;
}
But this has the disadvantage that you can only use one call to writeSpace in your printf arguments, as the second one will clean up the memory of the previous call. Still might be what you want.
BTW, The + (30-i) part is evil. What you probably want is this instead:
asprintf(ret, "%*s", i, ""); // Prints i spaces