Format strings into a expanding buffer.
struct sbprintf_string_buffer sb = { NULL, NULL, 0 };
sbprintf( &sb, "%d: %s\n", 1, "one" );
sbprintf( &sb, "%d: %s\n", 2, "two" );
sbprintf( &sb, "%d: %s\n", 3, "tree" );
sbprintf( &sb, "%d: %s\n", 4, "four" );
sbprintf( &sb, "%d: %s\n", 5, "five" );
printf( "%s", sb.base );
free( sb.base );
You may either call sbprintf() with a NULL'ed sbprintf_string_buffer structure (see sample above) and have it (re)allocate memory on every call or you may allocate a buffer of reasonable size beforehand:
struct sbprintf_string_buffer sb;
sb.length = 1024;
sb.base = sb.cursor = malloc( sb.length );
The first option is more memory efficient, the latter may perform better according on how many additional realloc()'s are required.