Yury Pogrebnyak Yury Pogrebnyak - 1 month ago 10
C++ Question

Build string as in printf function

printf("%d.%d.%d", year, month, day);


Can I do the same but without printing, smth like

char* date = "%d.%d.%d", year, month, day;


Or maybe some other simple ways to do that?

Answer

In plain c there is asprintf() which will allocate memory to hold the resulting string:

#include <stdio.h>
char *date;
asprintf(&date, "%d.%d.%d", year, month, day);

(error handling omitted)

Since you have tagged C++ you probably want to use the C++ solutions.

Comments